R语言 如何用另一个条形图覆盖条形图,但条形图的宽度较小?

utugiqy6  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(119)

我想在一个图中画出四个维度。对于每一个十分位数,我希望收入和支出并排。
此外,农村值应在背景中以正常的条宽显示,而城市值则以较窄的条宽作为覆盖显示在前景中。
收入条指的是左边的Y轴,支出指的是右边的Y轴。后面的颜色(农村区域)的alpha值应为0.5
我尝试了以下几点:

# Creates N colors for Viridis
n <- 22
color <- viridis(n)

# See and compare color scales:
show_col(viridis_pal()(n))

# Plot
ggplot(data, aes(fill = Decile, alpha = Divide)) + 
geom_col(data=data, aes(x=Decile, y=Avg.q.hou.inc), position = position_dodge(width = 0.9)) +
geom_col(data=data, aes(x=Decile, y=Avg.q.hou.exp), position = position_dodge(width = 0.9)) +
scale_fill_manual(values = c(color[1:10])) +
scale_alpha_manual(values = c(0.5, 1)) +
scale_y_continuous(labels = comma,
                   name = "Average quarterly household income",
                   sec.axis = sec_axis(~ ., 
                                       name = "Average quarterly household expenditure",
                                       labels = comma))

字符串
生成以下图形:


的数据
数据可在此处获得:https://drive.google.com/file/d/1pyFAWZ2CU7bUkzpCm-QjfsqFUAe47bFI/view?usp=sharing
图的示例如下:


z3yyvxxp

z3yyvxxp1#

你能做到的

barplot(t(dat[dat$Divide == 'Rural', 4:5]), border=NA, beside=TRUE, las=1,
        col=hcl.colors(2, alpha=.5, palette="Blue-Red 3" ), names=1:10, 
        xlab='Decile', ylab='USD')
barplot(t(dat[dat$Divide != 'Rural', 4:5]), border=NA, beside=TRUE, las=1,
        col=hcl.colors(2, palette="Blue-Red 3"), rep.int('', 10), 
        axes=F, xlab='Decile', ylab='USD', add=TRUE, width=.5, 
        space=c(2.5, rep.int(c(1, 3), 10)[-20]))
legend('topleft', pch=15, legend=c('Income', 'Expenditure', 'Urban', 'Rural'), 
       col=c(hcl.colors(2, alpha=.8, palette="Blue-Red 3" ), gray.colors(3)), 
       bty='n', ncol=2)

字符串


的数据
我会尽量缩短标签,并在标题中更详细地描述它们。可以使用?axis()添加第二个y轴,但这不是必需的,因为它具有相同的比例。

  • 数据:*
dat <- structure(list(Index = 0:19, Decile = c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), Divide = c("Rural", 
"Urban", "Rural", "Urban", "Rural", "Urban", "Rural", "Urban", 
"Rural", "Urban", "Rural", "Urban", "Rural", "Urban", "Rural", 
"Urban", "Rural", "Urban", "Rural", "Urban"), Avg.q.hou.inc = c(468.7513445, 
503.5536165, 836.6042491, 846.6231055, 1117.543736, 1121.113747, 
1388.068337, 1394.903172, 1684.565218, 1689.045482, 2027.501712, 
2035.265346, 2467.391966, 2470.373685, 3066.896226, 3078.378555, 
4070.91289, 4115.093085, 8415.39829, 8322.526202), Avg.q.hou.exp = c(538.6356851, 
665.0309443, 749.6479861, 824.6475602, 922.7627809, 979.5111549, 
1079.459555, 1168.296587, 1269.830962, 1374.957156, 1456.330907, 
1578.838944, 1761.175143, 1868.41908, 2112.405008, 2247.548134, 
2685.684306, 2943.071723, 4877.94356, 5660.834805), Avg.y.pcp.inc = c(955.227078, 
1312.82393, 1441.331452, 1864.090766, 1666.838773, 2173.690734, 
1927.282759, 2422.176835, 2159.373895, 2612.095502, 2460.237537, 
2914.67344, 2812.784898, 3408.482014, 3460.88369, 4026.771396, 
4415.684919, 5220.122704, 9820.029466, 10741.40859), Avg.y.pcp.exp = c(981.8982745, 
1514.34048, 1167.58913, 1632.639219, 1288.998903, 1742.214557, 
1435.197239, 1917.360561, 1583.779924, 2065.319638, 1724.220523, 
2204.66105, 1973.161724, 2560.15513, 2426.000606, 2941.233286, 
2962.063801, 3788.046584, 5758.785529, 7358.786349)), class = "data.frame", row.names = c(NA, 
-20L))

ffscu2ro

ffscu2ro2#

最后,我用下面的代码完成了我需要的图形:

ggplot(data, aes(alpha=Divide, linetype=Divide)) + 
  geom_col(data=data, 
           aes(x=Decile, y=Avg.q.hou.inc, color=Divide, fill=Decile), 
           position = position_dodge(1)) +
  geom_col(data=data, 
           aes(x=Decile, y=Avg.q.hou.exp, color=Divide, fill=Decile), 
           position = position_dodge(1)) +
  scale_fill_manual(values = c(color[1:10])) +
  scale_alpha_manual(values = c(0.5, 1), name="Domain") +
  scale_color_manual(values = c("black", "black")) +
  scale_y_continuous(labels = comma,
                     name="USD") +
  theme_classic(base_size = 15) +
  theme(legend.position = c(0.5, 1), 
        legend.box = 'vertical', 
        legend.direction = 'horizontal',
        legend.justification = c(0.5, 1),
        axis.text.x = element_text(size=15, color="black"),
        axis.text.y = element_text(size=15, color="black"),
        axis.title.x = element_text(size=15, color="black", vjust=-1),
        axis.title.y = element_text(size=15, color="black")) +
  guides(color="none",   # https://datavizpyr.com/selectively-remove-legends-with-ggplot2/
         linetype = "none",
         fill = guide_legend(title.position = "top", 
                             title.hjust = 0.5, 
                             label.hjust = 0.5,
                             nrow=1, 
                             byrow=TRUE,
                             order=1),
         alpha = guide_legend(name="Domain",
                              title.position = "top",
                              title.hjust = 0.5, 
                              label.hjust = 0.5,
                              nrow=1, 
                              byrow=TRUE,
                              order=2))

字符串
所得数字:


的数据

相关问题