我正在尝试绘制条形图,并在另一个y轴上测量重叠线形图。
我知道这不一定是您应该执行的操作,但这正是客户想要的。
我已经完成了大部分工作,但是我无法完成它。
这是我正在使用的数据框:
age var1 var2 count
1 18 0.6553120 0.05554554 100.00
2 19 0.4869745 0.65048383 50.00
3 20 0.2142351 0.98546390 25.00
4 21 0.3888707 0.04171436 12.50
5 22 0.2707270 0.13346492 6.25
6 23 0.7637131 0.21246432 3.12
然后将其重塑为长格式:
age count composite_col new_val
1 18 50.000 var1 0.65531200
2 19 25.000 var1 0.48697446
3 20 12.500 var1 0.21423509
4 21 6.250 var1 0.38887071
5 22 3.125 var1 0.27072698
6 23 1.560 var1 0.76371308
7 18 50.000 var2 0.05554554
8 19 25.000 var2 0.65048383
9 20 12.500 var2 0.98546390
10 21 6.250 var2 0.04171436
11 22 3.125 var2 0.13346492
12 23 1.560 var2 0.21246432
# mutate the counts so that we don't double count
df4 = df4 %>% mutate(count = count/(length(unique(composite_col))))
然后用这种怪兽作图:
ggplot(data = df4) + theme_classic() + geom_col( aes(x=age, y=count),width=1.0, fill="red", colour="black") +
geom_line( aes(x=age, y=new_val*100, colour=composite_col) ) +
scale_x_continuous( breaks = seq(18, 23,by = 1), expand = c(0,0)) +
scale_y_continuous("count", expand=c(0,0), sec.axis = sec_axis(~./100, name = "price", breaks = seq(0,2,by = 0.1) ))
不幸的是,它以堆积的方式绘制了年龄段的计数
完整代码在这里:
library(tidyr)
library(ggplot2)
set.seed(1911)
df2 = data.frame(age = c(18,19,20,21,22,23), var1 = runif(6,0,1), var2 = runif(6,0,1), count = c(100,50,25,12.5,6.25,3.12) )
df2
df4 = gather(df2, "composite_col","new_val",c(var1,var2))
df4
# mutate the counts so that we don't double count
df4 = df4 %>% mutate(count = count/(length(unique(composite_col))))
ggplot(data = df4) + theme_classic() + geom_col( aes(x=age, y=count),width=1.0, fill="red", colour="black") +
geom_line( aes(x=age, y=new_val*100, colour=composite_col) ) +
scale_x_continuous( breaks = seq(18, 23,by = 1), expand = c(0,0)) +
scale_y_continuous("count", expand=c(0,0), sec.axis = sec_axis(~./100, name = "price", breaks = seq(0,2,by = 0.1) ))