简体   繁体   English

R:在同一个图上绘制一条线和水平条形图

[英]R: plotting a line and horizontal barplot on the same plot

I am trying to combine a line plot and horizontal barplot on the same plot.我试图在同一个图上组合线图和水平条形图。 The difficult part is that the barplot is actually counts of the y values of the line plot.困难的部分是条形图实际上是线图的 y 值的计数。

Can someone show me how this can be done using the example below ?有人可以使用下面的示例告诉我如何做到这一点吗?

library(ggplot2)
library(plyr)
x <- c(1:100)
dff <- data.frame(x = x,y1 = sample(-500:500,size=length(x),replace=T), y2 = sample(3:20,size=length(x),replace=T))

counts <- ddply(dff, ~ y1, summarize, y2 = sum(y2))

# line plot
ggplot(data=dff) + geom_line(aes(x=x,y=y1)) 
# bar plot
ggplot() + geom_bar(data=counts,aes(x=y1,y=y2),stat="identity")  

I believe what I need is presented in the pseudocode below but I do not know how to write it out in R.我相信我需要的内容在下面的伪代码中给出,但我不知道如何用 R 写出来。

Apologies.道歉。 I actually meant the secondary x axis representing the value of counts for the barplot, while primary y-axis is the y1 .我实际上是指表示条形图计数值的次要 x 轴,而主要 y 轴是 y1

ggplot(data=dff) + geom_line(aes(x=x,y=y1)) + geom_bar(data=counts , aes(primary y axis = y1,secondary x axis =y2),stat="identity")  

在此处输入图片说明

I just want the barplots to be plotted horizontally, so I tried the code below which flip both the line chart and barplot, which is also not I wanted.我只想水平绘制条形图,所以我尝试了下面的代码,它同时翻转折线图和条形图,这也不是我想要的。

ggplot(data=dff) + 
  geom_line(aes(x=x,y=y1)) + 
  geom_bar(data=counts,aes(x=y2,y=y1),stat="identity") + coord_flip()

在此处输入图片说明

You can combine two plots in ggplot like you want by specifying different data = arguments in each geom_ layer (and none in the original ggplot() call).您可以通过在每个geom_层中指定不同的data =参数(并且在原始ggplot()调用中没有),将两个图组合在ggplot

 ggplot() + 
    geom_line(data=dff, aes(x=x,y=y1)) + 
    geom_bar(data=counts,aes(x=y1,y=y2),stat="identity") 

The following plot is the result.下图是结果。 However, since x and y1 have different ranges, are you sure this is what you want?但是,由于xy1具有不同的范围,您确定这是您想要的吗?

在此处输入图片说明

Perhaps you want y1 on the vertical axis for both plots.也许您希望y1在两个图的垂直轴上。 Something like this works:像这样的工作:

ggplot() + geom_line(data=dff, aes(x=y1 ,y = x)) + ggplot() + geom_line(data=dff, aes(x=y1,y = x)) +
geom_bar(data=counts,aes(x=y1,y=y2),stat="identity", color = "red") + coord_flip() geom_bar(data=counts,aes(x=y1,y=y2),stat="identity", color = "red") + coord_flip()

在此处输入图片说明

Maybe you are looking for this.也许你正在寻找这个。 Ans based on your last code you look for a double axis. Ans 根据您的最后一个代码寻找双轴。 So using dplyr you can store the counts in the same dataframe and then plot all variables.因此,使用dplyr您可以将计数存储在同一个数据dplyr ,然后绘制所有变量。 Here the code:这里的代码:

library(ggplot2)
library(dplyr)
#Data
x <- c(1:100)
dff <- data.frame(x = x,y1 = sample(-500:500,size=length(x),replace=T), y2 = sample(3:20,size=length(x),replace=T))
#Code
dff %>% group_by(y1) %>% mutate(Counts=sum(y2)) -> dff2
#Scale factor
sf <- max(dff2$y1)/max(dff2$Counts)
# Plot
ggplot(data=dff2)+
  geom_line(aes(x=x,y=y1),color='blue',size=1)+
  geom_bar(stat='identity',aes(x=x,y=Counts*sf),fill='tomato',color='black')+
  scale_y_continuous(name="y1", sec.axis = sec_axis(~./sf, name="Counts"))

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM