Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用相同的y轴绘制两个不同的ggplot2图表_R_Ggplot2 - Fatal编程技术网

R 使用相同的y轴绘制两个不同的ggplot2图表

R 使用相同的y轴绘制两个不同的ggplot2图表,r,ggplot2,R,Ggplot2,我想使用ggplot2使用一个公共y轴并排绘制两个不同的图表: p1)使用geom_rect()的“面积矩形”图 p2)使用geom_density()的密度图 通过使用grid.arrange(),我几乎做到了这一点,但y轴并没有完全对齐。我认为facet\u wrap或facet\u grid在这里不起作用,但我可能错了 rm(list=ls()) library(ggplot2) library(gridExtra) library(dplyr) df<-structure(lis

我想使用ggplot2使用一个公共y轴并排绘制两个不同的图表:

p1)使用geom_rect()的“面积矩形”图

p2)使用geom_density()的密度图

通过使用grid.arrange(),我几乎做到了这一点,但y轴并没有完全对齐。我认为facet\u wrap或facet\u grid在这里不起作用,但我可能错了

rm(list=ls())
library(ggplot2)
library(gridExtra)
library(dplyr)

df<-structure(list(total_ulr = c(0.442, 0.679, 0.74, 0.773, 0.777, 
                                 0.8036, 0.87, 0.871, 0.895, 0.986, 1.003, 1.2054, 1.546, 1.6072
), width = c(4222L, 14335L, 2572L, 2460L, 1568L, 8143L, 3250L, 
             17119L, 3740L, 3060L, 2738L, 1L, 1L, 790L), w = c(4222L, 18557L, 
                                                               21129L, 23589L, 25157L, 33300L, 36550L, 53669L, 57409L, 60469L, 
                                                               63207L, 63208L, 63209L, 63999L), wm = c(0L, 4222L, 18557L, 21129L, 
                                                                                                       23589L, 25157L, 33300L, 36550L, 53669L, 57409L, 60469L, 63207L, 
                                                                                                       63208L, 63209L), wt = c(2111, 11389.5, 19843, 22359, 24373, 29228.5, 
                                                                                                                               34925, 45109.5, 55539, 58939, 61838, 63207.5, 63208.5, 63604), 
mainbuckets = c(" 4,222", "14,335", " 2,572", " 2,460", " 1,568", 
                " 8,143", " 3,250", "17,119", " 3,740", " 3,060", " 2,738", 
                "", "", "   790"), mainbucketsULR = c("0.44", "0.68", "0.74", 
                                                      "0.77", "0.78", "0.80", "0.87", "0.87", "0.90", "0.99", "1.00", 
                                                      "", "", "1.61")), .Names = c("total_ulr", "width", "w", "wm", 
                                                                                   "wt", "mainbuckets", "mainbucketsULR"), class = c("tbl_df", "tbl", 
                                                                                                                                     "data.frame"), row.names = c(NA, -14L))

textsize<-4

p1<-
  ggplot(df, aes(ymin=0)) + 
  geom_rect(aes(xmin = wm, xmax = w, ymax = total_ulr, fill = total_ulr)) +
  scale_x_reverse() +
  geom_text(aes(x = wt, y = total_ulr+0.02, label = mainbuckets),size=textsize,color="black") +
  geom_text(aes(x = wt, y = 0.02, label = mainbucketsULR),size=textsize+1,color="white",hjust=0,angle=90) +
  xlab("Frequency") +
  ylab("Ratio") +
  ggtitle(paste("My Title")) +
  theme_bw() +
  theme(legend.position = "none"
        ,axis.text.x=element_blank())


p2<-ggplot(df, aes(total_ulr,fill=width,ymin=0)) + geom_density(color="grey",fill="grey") +
  ggtitle("Density") +
  xlab("") +
  ylab("") +
  theme_bw() +
  coord_flip()+scale_y_reverse() +
  theme(text=element_text(size=10)
        ,axis.text.x=element_blank()
        ,legend.position="none"
        #,axis.text.y=element_blank()
  )

grid.arrange(p2.common.y,p1.common.y,ncol=2,widths=c(1,5))
rm(list=ls())
图书馆(GG2)
图书馆(gridExtra)
图书馆(dplyr)

给你。您需要使用
scale\u y.*
函数定义一个公共轴,然后设置要匹配的打印高度

limits <- c(0, 2)
breaks <- seq(limits[1], limits[2], by=.5)

# assign common axis to both plots
p1.common.y <- p1 + scale_y_continuous(limits=limits, breaks=breaks)
p2.common.y <- p2 + scale_x_continuous(limits=limits, breaks=breaks)

# At this point, they have the same axis, but the axis lengths are unequal, so ...

# build the plots 
p1.common.y <- ggplot_gtable(ggplot_build(p1.common.y))
p2.common.y <- ggplot_gtable(ggplot_build(p2.common.y))

# copy the plot height from p1 to p2
p2.common.y$heights <- p1.common.y$heights

grid.arrange(p2.common.y,p1.common.y,ncol=2,widths=c(1,5))

限制给你。您需要使用
scale\u y.*
函数定义一个公共轴,然后设置要匹配的打印高度

limits <- c(0, 2)
breaks <- seq(limits[1], limits[2], by=.5)

# assign common axis to both plots
p1.common.y <- p1 + scale_y_continuous(limits=limits, breaks=breaks)
p2.common.y <- p2 + scale_x_continuous(limits=limits, breaks=breaks)

# At this point, they have the same axis, but the axis lengths are unequal, so ...

# build the plots 
p1.common.y <- ggplot_gtable(ggplot_build(p1.common.y))
p2.common.y <- ggplot_gtable(ggplot_build(p2.common.y))

# copy the plot height from p1 to p2
p2.common.y$heights <- p1.common.y$heights

grid.arrange(p2.common.y,p1.common.y,ncol=2,widths=c(1,5))

谢谢你,马修。当我从头开始重新运行数据和代码时,会收到一条错误消息和一个空白绘图:p2.common.y ggplot_表绝对是正确的选择,感谢您的说明。我已经用错误消息和回溯()更新了问题。我认为错误在于“p2+比例x(极限=极限,断裂=断裂)”。我现在看到的是,它是scale_x_reverse(因为我已经在p2中应用了scale_y_reverse)。我编辑了p2+比例x连续的答案。再次感谢Matthew.From?ggplotgtable:“此函数构建显示绘图所需的所有GROB,并将它们存储在称为gtable的特殊数据结构中。如果您愿意,此对象可以进行编程操作(例如:将图例框设为2厘米宽,或将多个绘图合并为一个显示器,保留绘图的纵横比。”谢谢,马修。当我从头开始重新运行数据和代码时,会收到一条错误消息和一个空白绘图:p2.common.y ggplot_表绝对是正确的选择,感谢您的说明。我已经用错误消息和回溯()更新了问题。我认为错误在于“p2+比例x(极限=极限,断裂=断裂)”。我现在看到的是,它是scale_x_reverse(因为我已经在p2中应用了scale_y_reverse)。我编辑了p2+比例x连续的答案。再次感谢Matthew.From?ggplotgtable:“此函数构建显示绘图所需的所有GROB,并将它们存储在称为gtable的特殊数据结构中。如果您愿意,此对象可以进行编程操作(例如:将图例框设置为2 cm宽,或将多个绘图合并为一个显示,以保持绘图的纵横比。”