如何对齐一组条形图中的标签?
How can I align labels in a group of bar charts?
我想对齐一组条形图中各列的计数和百分比标签。在该组的某些图表中,并未显示所有值。情绪有三种价值——积极、中立和消极——但有些办公室只有一种或两种回应。对于没有值的单个图表,标签不对齐。如何使列和标签对齐。
这是数据和我的代码——非常感谢支持!
data
office sentiment
1 positive
1 positive
1 neutral
1 neutral
1 positive
1 positive
1 positive
1 positive
1 neutral
1 neutral
1 neutral
1 positive
1 positive
1 negative
1 negative
1 positive
1 neutral
1 neutral
1 neutral
1 positive
1 neutral
1 neutral
1 negative
1 positive
1 positive
1 neutral
2 positive
2 positive
2 neutral
3 positive
3 positive
3 positive
3 positive
4 positive
4 negative
4 neutral
5 positive
6 positive
6 positive
6 positive
6 positive
6 neutral
6 positive
6 positive
6 positive
6 positive
7 positive
8 neutral
8 positive
8 positive
df <- [office, sentiment] # a data frame or tibble of the above data set
office_sentiment <- df[ , c("office","sentiment")]
office_sentiment <- office_sentiment %>% group_by(sentiment,office) %>% summarize(count = n())
office_sentiment <- filter(office_sentiment,count >= 1,sentiment != "NA") #%>%
office_sentiment
office_sentiment_percentage <- df[ , c("office","sentiment")]
office_sentiment_percentage <- office_sentiment_percentage %>% group_by(sentiment,office) %>% summarize(count = n())
office_sentiment_percentage <- filter(office_sentiment_percentage,count >= 1,sentiment != "NA") %>%
mutate(percentage=count/sum(count)*100)
office_sentiment_percentage$percentage <- paste0(round(office_sentiment_percentage$percentage,1),"%")
# the function i use for most bar charts
myBarChart_2 <- function(data,var1,var2,count,title,xLabel,yLabel) {
ggplot(data, aes_string(x=var1, y=count, fill=var2)) +
ggtitle(title) +
geom_bar(stat = 'idoffice',width=1,position = position_dodge2(padding=0.1,reverse=FALSE,preserve=c("single"))) +
scale_color_manual(values=c("#66CCFF","#009999","#FF66CC"),aesthetics = c("colour", "fill")) +
scale_y_continuous(sec.axis=waiver(),expand = expansion(mult = c(0,0.05))) +
facet_wrap(var1, strip.position="bottom",scales = "free_x") +
xlab(xLabel) +
ylab(yLabel) +
theme_set(myBar_theme) +[![enter image description here][1]][1]
theme(axis.text.x=element_blank())
}
# code for this sample set of charts
myBarChart_2(office_sentiment_percentage,'officeType','sentiment','count',"Sentiment by Office","sentiment","total") +
(aes(group=factor(sentiment, levels=c('positive','neutral','negative')))) +
geom_text(aes(label = count),vjust=1.5,position=position_dodge2(width=1),size=2.5) +
geom_text(aes(label = percentage),vjust=-.3,position=position_dodge2(width=1),size=2.5) +
guides(fill = guide_legend(reverse = TRUE))
您的最小可重现示例存在许多问题。我强烈建议在以后的帖子中使用 reprex package and following How to make a minimal reproducible example and How to make a great R reproducible example。
我相信,您的问题的答案非常简单。如果您将 preserve = "single"
添加到每个 geom_text()
,标签的位置似乎正确。我无法 运行 你的示例代码,所以我删除了一些有问题的部分来说明我的答案:
library(tidyverse)
df <- structure(list(office = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 8L, 8L, 8L),
sentiment = c("positive",
"positive", "neutral", "neutral", "positive", "positive", "positive",
"positive", "neutral", "neutral", "neutral", "positive", "positive",
"negative", "negative", "positive", "neutral", "neutral", "neutral",
"positive", "neutral", "neutral", "negative", "positive", "positive",
"neutral", "positive", "positive", "neutral", "positive", "positive",
"positive", "positive", "positive", "negative", "neutral", "positive",
"positive", "positive", "positive", "positive", "neutral", "positive",
"positive", "positive", "positive", "positive", "neutral", "positive",
"positive")), class = "data.frame", row.names = c(NA, -50L))
office_sentiment <- df[ , c("office","sentiment")]
office_sentiment <- office_sentiment %>% group_by(sentiment,office) %>% summarize(Count = n())
#> `summarise()` has grouped output by 'sentiment'. You can override using the `.groups` argument.
office_sentiment <- filter(office_sentiment, Count >= 1,sentiment != "NA") #%>%
office_sentiment
#> # A tibble: 15 × 3
#> # Groups: sentiment [3]
#> sentiment office Count
#> <chr> <int> <int>
#> 1 negative 1 3
#> 2 negative 4 1
#> 3 neutral 1 11
#> 4 neutral 2 1
#> 5 neutral 4 1
#> 6 neutral 6 1
#> 7 neutral 8 1
#> 8 positive 1 12
#> 9 positive 2 2
#> 10 positive 3 4
#> 11 positive 4 1
#> 12 positive 5 1
#> 13 positive 6 8
#> 14 positive 7 1
#> 15 positive 8 2
office_sentiment_percentage <- df[ , c("office","sentiment")]
office_sentiment_percentage <- office_sentiment_percentage %>% group_by(sentiment,office) %>% summarize(Count = n())
#> `summarise()` has grouped output by 'sentiment'. You can override using the `.groups` argument.
office_sentiment_percentage <- filter(office_sentiment_percentage, Count >= 1,sentiment != "NA") %>%
mutate(percentage=Count/sum(Count)*100)
office_sentiment_percentage$percentage <- paste0(round(office_sentiment_percentage$percentage,1),"%")
ggplot(office_sentiment_percentage,
aes(x = office, y = Count, fill = sentiment)) +
ggtitle("Sentiment by Office") +
geom_col(width = 1,
position = position_dodge2(
padding = 0.1,
reverse = FALSE,
preserve = c("single")
)) +
scale_color_manual(
values = c("#66CCFF", "#009999", "#FF66CC"),
aesthetics = c("colour", "fill")
) +
scale_y_continuous(sec.axis = waiver(), expand = expansion(mult = c(0, 0.05))) +
facet_wrap(~office, strip.position = "bottom", scales = "free_x") +
xlab("sentiment") +
ylab("total count") +
theme(axis.text.x = element_blank()) +
geom_text(
aes(label = Count),
vjust = 1.5,
position = position_dodge2(width = 1, preserve = "single"),
size = 2.5
) +
geom_text(
aes(label = percentage),
vjust = -.3,
position = position_dodge2(width = 1, preserve = "single"),
size = 2.5
) +
guides(fill = guide_legend(reverse = TRUE))
由 reprex package (v2.0.1)
创建于 2022-01-14
这是否解决了您的问题?
我想对齐一组条形图中各列的计数和百分比标签。在该组的某些图表中,并未显示所有值。情绪有三种价值——积极、中立和消极——但有些办公室只有一种或两种回应。对于没有值的单个图表,标签不对齐。如何使列和标签对齐。
这是数据和我的代码——非常感谢支持!
data
office sentiment
1 positive
1 positive
1 neutral
1 neutral
1 positive
1 positive
1 positive
1 positive
1 neutral
1 neutral
1 neutral
1 positive
1 positive
1 negative
1 negative
1 positive
1 neutral
1 neutral
1 neutral
1 positive
1 neutral
1 neutral
1 negative
1 positive
1 positive
1 neutral
2 positive
2 positive
2 neutral
3 positive
3 positive
3 positive
3 positive
4 positive
4 negative
4 neutral
5 positive
6 positive
6 positive
6 positive
6 positive
6 neutral
6 positive
6 positive
6 positive
6 positive
7 positive
8 neutral
8 positive
8 positive
df <- [office, sentiment] # a data frame or tibble of the above data set
office_sentiment <- df[ , c("office","sentiment")]
office_sentiment <- office_sentiment %>% group_by(sentiment,office) %>% summarize(count = n())
office_sentiment <- filter(office_sentiment,count >= 1,sentiment != "NA") #%>%
office_sentiment
office_sentiment_percentage <- df[ , c("office","sentiment")]
office_sentiment_percentage <- office_sentiment_percentage %>% group_by(sentiment,office) %>% summarize(count = n())
office_sentiment_percentage <- filter(office_sentiment_percentage,count >= 1,sentiment != "NA") %>%
mutate(percentage=count/sum(count)*100)
office_sentiment_percentage$percentage <- paste0(round(office_sentiment_percentage$percentage,1),"%")
# the function i use for most bar charts
myBarChart_2 <- function(data,var1,var2,count,title,xLabel,yLabel) {
ggplot(data, aes_string(x=var1, y=count, fill=var2)) +
ggtitle(title) +
geom_bar(stat = 'idoffice',width=1,position = position_dodge2(padding=0.1,reverse=FALSE,preserve=c("single"))) +
scale_color_manual(values=c("#66CCFF","#009999","#FF66CC"),aesthetics = c("colour", "fill")) +
scale_y_continuous(sec.axis=waiver(),expand = expansion(mult = c(0,0.05))) +
facet_wrap(var1, strip.position="bottom",scales = "free_x") +
xlab(xLabel) +
ylab(yLabel) +
theme_set(myBar_theme) +[![enter image description here][1]][1]
theme(axis.text.x=element_blank())
}
# code for this sample set of charts
myBarChart_2(office_sentiment_percentage,'officeType','sentiment','count',"Sentiment by Office","sentiment","total") +
(aes(group=factor(sentiment, levels=c('positive','neutral','negative')))) +
geom_text(aes(label = count),vjust=1.5,position=position_dodge2(width=1),size=2.5) +
geom_text(aes(label = percentage),vjust=-.3,position=position_dodge2(width=1),size=2.5) +
guides(fill = guide_legend(reverse = TRUE))
您的最小可重现示例存在许多问题。我强烈建议在以后的帖子中使用 reprex package and following How to make a minimal reproducible example and How to make a great R reproducible example。
我相信,您的问题的答案非常简单。如果您将 preserve = "single"
添加到每个 geom_text()
,标签的位置似乎正确。我无法 运行 你的示例代码,所以我删除了一些有问题的部分来说明我的答案:
library(tidyverse)
df <- structure(list(office = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 8L, 8L, 8L),
sentiment = c("positive",
"positive", "neutral", "neutral", "positive", "positive", "positive",
"positive", "neutral", "neutral", "neutral", "positive", "positive",
"negative", "negative", "positive", "neutral", "neutral", "neutral",
"positive", "neutral", "neutral", "negative", "positive", "positive",
"neutral", "positive", "positive", "neutral", "positive", "positive",
"positive", "positive", "positive", "negative", "neutral", "positive",
"positive", "positive", "positive", "positive", "neutral", "positive",
"positive", "positive", "positive", "positive", "neutral", "positive",
"positive")), class = "data.frame", row.names = c(NA, -50L))
office_sentiment <- df[ , c("office","sentiment")]
office_sentiment <- office_sentiment %>% group_by(sentiment,office) %>% summarize(Count = n())
#> `summarise()` has grouped output by 'sentiment'. You can override using the `.groups` argument.
office_sentiment <- filter(office_sentiment, Count >= 1,sentiment != "NA") #%>%
office_sentiment
#> # A tibble: 15 × 3
#> # Groups: sentiment [3]
#> sentiment office Count
#> <chr> <int> <int>
#> 1 negative 1 3
#> 2 negative 4 1
#> 3 neutral 1 11
#> 4 neutral 2 1
#> 5 neutral 4 1
#> 6 neutral 6 1
#> 7 neutral 8 1
#> 8 positive 1 12
#> 9 positive 2 2
#> 10 positive 3 4
#> 11 positive 4 1
#> 12 positive 5 1
#> 13 positive 6 8
#> 14 positive 7 1
#> 15 positive 8 2
office_sentiment_percentage <- df[ , c("office","sentiment")]
office_sentiment_percentage <- office_sentiment_percentage %>% group_by(sentiment,office) %>% summarize(Count = n())
#> `summarise()` has grouped output by 'sentiment'. You can override using the `.groups` argument.
office_sentiment_percentage <- filter(office_sentiment_percentage, Count >= 1,sentiment != "NA") %>%
mutate(percentage=Count/sum(Count)*100)
office_sentiment_percentage$percentage <- paste0(round(office_sentiment_percentage$percentage,1),"%")
ggplot(office_sentiment_percentage,
aes(x = office, y = Count, fill = sentiment)) +
ggtitle("Sentiment by Office") +
geom_col(width = 1,
position = position_dodge2(
padding = 0.1,
reverse = FALSE,
preserve = c("single")
)) +
scale_color_manual(
values = c("#66CCFF", "#009999", "#FF66CC"),
aesthetics = c("colour", "fill")
) +
scale_y_continuous(sec.axis = waiver(), expand = expansion(mult = c(0, 0.05))) +
facet_wrap(~office, strip.position = "bottom", scales = "free_x") +
xlab("sentiment") +
ylab("total count") +
theme(axis.text.x = element_blank()) +
geom_text(
aes(label = Count),
vjust = 1.5,
position = position_dodge2(width = 1, preserve = "single"),
size = 2.5
) +
geom_text(
aes(label = percentage),
vjust = -.3,
position = position_dodge2(width = 1, preserve = "single"),
size = 2.5
) +
guides(fill = guide_legend(reverse = TRUE))
由 reprex package (v2.0.1)
创建于 2022-01-14这是否解决了您的问题?