首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中创建和合并两个图- xy线形图和条形链图

在R中创建和合并两个图- xy线形图和条形链图
EN

Stack Overflow用户
提问于 2012-02-18 18:44:14
回答 1查看 474关注 0票数 2

下面两个数据集是我打算用来创建图形的:

第一个数据(将开发底部)

代码语言:javascript
复制
position <- c(10, 26, 31, 50, 73, 92, 120, 124)  # need scale 
    minimum 0 to maximum 130
label <- c("A", "B", "C", "D", "E", "F", "G", "H")
mydf <- data.frame (position, label)

第二个数据(将在叠层上绘制线条图)

代码语言:javascript
复制
pos <- 1:130
value <- seq (0, 1.29, 0.01) 
mydf2 <- data.frame (pos, value) 

图要开发(类似或更高质量):

我的试验

下面就是我尝试过的,完全划破!

代码语言:javascript
复制
yvar <- rep(1, length(position))

require (ggplot2)


bar <- data.frame(y = c(1, 1), x = c(0, 130))
ggplot() +
  geom_line(aes(x, factor(y), group = factor(y)),
            bar, size = 2, colour = "skyblue") +
  geom_rect(aes(y = yvar,
                 xmin = position - 0.1,
                 xmax = position + 0.1,
                 ymin = 1 - yvar /2,
                 ymax = 1 + yvar /2))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-18 19:46:52

这是一个使用基础图形的解决方案。

代码语言:javascript
复制
# Split the plot area in two
layout(matrix(c(1,1,2),nc=1))
# First plot
plot( pos, value, type="l", las=1 )
# Reduce the margins for the second plot
m <- par()$mar
m[1] <- m[3] <- 0
par(mar=m)
# Set the limits of the second plot
plot( pos, pos-pos, type="n", axes=FALSE, xlab="", ylab="" )
# Add the rectangle, the segments and the text.
polygon( 
  c(0,max(mydf2$pos),max(mydf2$pos),0), 
  .2*c(-1,-1,1,1),
  col=rgb(.1,.5,.3)
)
segments( mydf$position, -.5, mydf$position, .5 )
text(mydf$position, -.7, mydf$label)
text(mydf$position,  .7, mydf$position)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9340490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档