繁体   English   中英

geom_bar + geom_line:具有不同的y轴刻度?

[英]geom_bar + geom_line: with different y-axis scale?

有没有办法用geom_line绘制geom_bar,如下图所示。

在此输入图像描述

我想出了两个独立的图表。 如何将它们分别与左右两侧的两个不同轴组合。

library(ggplot2)
temp = data.frame(Product=as.factor(c("A","B","C")),
                  N = c(17100,17533,6756),
                  n = c(5,13,11),
                  rate = c(0.0003,0.0007,0.0016),
                  labels = c(".03%",".07%",".16%"))

p1 = ggplot(data = temp, aes(x=Product,y=N))+
  geom_bar(stat="identity",fill="#F8766D")+geom_text(aes(label=n,col="red",vjust=-0.5))+
  theme(legend.position="none",axis.title.y=element_blank(),axis.text.x = element_text(angle = 90, hjust = 1))
  p1
p2 = ggplot(data = temp,aes(x=Product,y=rate))+
  geom_line(aes(group=1))+geom_text(aes(label=labels,col="red",vjust=0))+
  theme(legend.position="none",axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 0))+
  xlab("Product")
p2

非常感谢。

现在ggplot2已经添加了对辅助轴的支持(从版本2.2.0开始),可以在单个ggplot()调用中创建这样的图形,代码少得多(没有堆叠多个图作为变通方法!)

ggplot(data = temp, aes(x = Product, y = N)) + #start plot by by plotting bars
  geom_bar(stat = "identity") + 
  #plot line on same graph
  # rate multiplied by 10000000 to get on same scale as bars
  geom_line(data = temp, aes(x = Product, y = (rate)*10000000, group = 1), 
            inherit.aes = FALSE) +
  #specify secondary axis
  #apply inverse of above transformation to correctly scale secondary axis (/10000000)
  scale_y_continuous(sec.axis = sec_axis(~./10000000, name = "rate"))

在此输入图像描述

我知道这是一个较老的问题,有一个答案,但想提供一个更新 - 由于软件包更新,有一个比接受答案(这是当时最好的解决方案)更简单的解决方案。

我从这里借用了大部分代码:

library(ggplot2)
library(gtable)
library(grid)

temp = data.frame(Product=as.factor(c("A","B","C")),
                  N = c(17100,17533,6756),
                  n = c(5,13,11),
                  rate = c(0.0003,0.0007,0.0016),
                  labels = c(".03%",".07%",".16%"))

p1 = ggplot(data = temp, aes(x=Product,y=N))+
  geom_bar(stat="identity",fill="#F8766D") + 
  geom_text(aes(label=n,col="red",vjust=-0.5))+
  theme(legend.position="none",axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 1))
p2 = ggplot(data = temp,aes(x=Product,y=rate))+
  geom_line(aes(group=1))+geom_text(aes(label=labels,vjust=0))+
  theme(legend.position="none",axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 0), 
        panel.background = element_rect(fill = NA), 
        panel.grid = element_blank())+
  xlab("Product")

g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

# draw it
grid.draw(g)

我从第二个图中删除了网格(它显示在顶部,看起来很乱)。

在此输入图像描述

暂无
暂无

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

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