如何在R中向geom_col()添加线
如何将一条包含个人总数的线(或者更好,将该线下的区域作为背景进行着色)添加到由以下人员生成的每个图形中:如何在R中向geom_col()添加线,r,ggplot2,R,Ggplot2,如何将一条包含个人总数的线(或者更好,将该线下的区域作为背景进行着色)添加到由以下人员生成的每个图形中: library(ggplot2) library(readxl) library(dplyr) library(RColorBrewer) dataset %>% ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
library(ggplot2)
library(readxl)
library(dplyr)
library(RColorBrewer)
dataset %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_col() +
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
我尝试了很多版本的geom_hline,但都没有成功
我的数据集是:
region office Nr_Individuals total_Nr_Individuals PovertyDecile weighted_PovDecile
1 A 80 105 2 2.5
1 A 23 105 3 2.5
1 B 2 105 4 4
2 C 100 160 1 1.5
2 C 60 160 2 1.5
3 D 20 70 8 9
3 D 50 70 10 9
我用代码4得到了什么(有什么想法吗?):
不带灰色选项:
尝试使用
geom_segment()
library(ggplot2)
#Code
df %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_col() +
geom_segment(aes(x=factor(PovertyDecile),xend=factor(PovertyDecile),yend=total_Nr_Individuals))+
geom_point(aes(x=factor(PovertyDecile),y=total_Nr_Individuals,fill=factor(PovertyDecile)),
shape=21,show.legend = F)+
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
输出:
使用的一些数据:
#Data
df <- structure(list(region = c(1L, 1L, 1L, 2L, 2L, 3L, 3L), office = c("A",
"A", "B", "C", "C", "D", "D"), Nr_Individuals = c(80L, 23L, 2L,
100L, 60L, 20L, 50L), total_Nr_Individuals = c(105L, 105L, 105L,
160L, 160L, 70L, 70L), PovertyDecile = c(2L, 3L, 4L, 1L, 2L,
8L, 10L), weighted_PovDecile = c(2.5, 2.5, 4, 1.5, 1.5, 9, 9)), class = "data.frame", row.names = c(NA,
-7L))
输出:
对于着色样式,您可以将此技巧用于其他geom\u col()
但调整alpha
:
#Code 3
df %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_errorbar(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals,ymin=total_Nr_Individuals,
ymax=total_Nr_Individuals,color=factor(PovertyDecile)))+
geom_col(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals,
fill=factor(PovertyDecile)),alpha=0.5) +
geom_col() +
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
scale_color_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
输出:
更新:
#Code 4
df %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_errorbar(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals,ymin=total_Nr_Individuals,
ymax=total_Nr_Individuals))+
geom_col(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals
),alpha=0.6,fill='gray') +
geom_col() +
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
输出:
对于连续线路:
#Code 5
df %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_hline(aes(yintercept=total_Nr_Individuals))+
geom_col(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals
),alpha=0.6,fill='gray') +
geom_col() +
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
scale_color_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
输出:
非常感谢,这很酷-但我有很多“办公室”,所以用这些观点来看它们可能会让人困惑。。。我试图找到一种方法来画一条线或在线下阴影区域作为每个“办公室”的背景-有什么想法吗?@DanielaRodrigues你是说每个酒吧都有一条水平线吗?每个“办公室”都有一条水平线,所以高于所有酒吧。理想的情况下,我想阴影下的线的x轴。。不确定是否可能?这正是我需要的-非常感谢。最后一点,我修改了#代码2以获得一条黑线,因此我有:“缩放颜色”手册(值=c(“黑色”、“黑色”、“黑色”、“黑色”、“黑色”、“黑色”)))“如何将阴影的颜色更改为纯灰色?@DanielaRodrigues我为您寻找的任务添加了额外的更新。希望有帮助!
#Code 5
df %>%
ggplot(.,aes(x=factor(PovertyDecile),y=Nr_Individuals,fill=factor(PovertyDecile))) +
geom_hline(aes(yintercept=total_Nr_Individuals))+
geom_col(aes(x=factor(PovertyDecile),
y=total_Nr_Individuals
),alpha=0.6,fill='gray') +
geom_col() +
facet_wrap(~ office)+
scale_fill_brewer(palette = "RdYlGn")+
scale_color_brewer(palette = "RdYlGn")+
theme_classic()+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)