geom_bar + geom_line:具有不同的y轴比例? [英] geom_bar + geom_line: with different y-axis scale?

查看:235
本文介绍了geom_bar + geom_line:具有不同的y轴比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法像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))+
主题(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

非常感谢。 p>

解决方案

我借了大部分的代码来自


Is there any way to plot geom_bar with geom_line like the following chart.

I have come up with the two separate charts. How to combine them with two different axes on the left and right sides respectively.

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

Thanks a lot.

解决方案

I'm borrowing most of the code from here:

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)

I removed the grid from the second plot (it appears on top and looks messy).

这篇关于geom_bar + geom_line:具有不同的y轴比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆