双 geom_bar,如何获取每个柱的值
double geom_bar, how to get the values for each bar
我有两个不同时间段(Y 轴)的国家(X 轴)的 ggplot,所以每个国家都有双条。
我想看看每个柱的值。我使用了 geom_text 但我在同一行上得到了值,所以它们不在位。我怎样才能使用 geom_text 这种类型的情节?
Rcountry %>%
gather("Type", "Value",-Country) %>%
ggplot(aes(Country, Value, fill = Type)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip()+
theme_minimal()+scale_fill_grey()+
theme(legend.position="bottom")+
theme(legend.title = element_blank())+
scale_fill_manual(values=c("darkslategray4", "darkslategrey"))+
labs(x="Country", y="Stock of robots per thousands worker in '000")+
geom_text(aes(label=c(X2010, X2018)), size=3.5)```
Thank you
这可以通过将 position = position_dodge(.9)
添加到 geom_text
来实现,即您必须使用 geom_bar
到 geom_text
中使用的定位才能使标签正确。使用 mtcars
作为示例数据,试试这个:
library(ggplot2)
library(dplyr)
mtcars2 <- mtcars %>%
group_by(cyl, gear) %>%
summarise(mpg = mean(mpg)) %>%
ungroup()
ggplot(mtcars2, aes(x = factor(cyl), mpg, fill = factor(gear))) +
geom_bar(position = "dodge", stat = "identity") +
theme_minimal() +
scale_fill_grey() +
theme(legend.position="bottom")+
theme(legend.title = element_blank())+
labs(x="Country", y="Stock of robots per thousands worker in '000")+
geom_text(aes(label = mpg), position = position_dodge(.9), size=3.5) +
coord_flip()
由 reprex package (v0.3.0)
于 2020-04-15 创建
我有两个不同时间段(Y 轴)的国家(X 轴)的 ggplot,所以每个国家都有双条。 我想看看每个柱的值。我使用了 geom_text 但我在同一行上得到了值,所以它们不在位。我怎样才能使用 geom_text 这种类型的情节?
Rcountry %>%
gather("Type", "Value",-Country) %>%
ggplot(aes(Country, Value, fill = Type)) +
geom_bar(position = "dodge", stat = "identity") +
coord_flip()+
theme_minimal()+scale_fill_grey()+
theme(legend.position="bottom")+
theme(legend.title = element_blank())+
scale_fill_manual(values=c("darkslategray4", "darkslategrey"))+
labs(x="Country", y="Stock of robots per thousands worker in '000")+
geom_text(aes(label=c(X2010, X2018)), size=3.5)```
Thank you
这可以通过将 position = position_dodge(.9)
添加到 geom_text
来实现,即您必须使用 geom_bar
到 geom_text
中使用的定位才能使标签正确。使用 mtcars
作为示例数据,试试这个:
library(ggplot2)
library(dplyr)
mtcars2 <- mtcars %>%
group_by(cyl, gear) %>%
summarise(mpg = mean(mpg)) %>%
ungroup()
ggplot(mtcars2, aes(x = factor(cyl), mpg, fill = factor(gear))) +
geom_bar(position = "dodge", stat = "identity") +
theme_minimal() +
scale_fill_grey() +
theme(legend.position="bottom")+
theme(legend.title = element_blank())+
labs(x="Country", y="Stock of robots per thousands worker in '000")+
geom_text(aes(label = mpg), position = position_dodge(.9), size=3.5) +
coord_flip()
由 reprex package (v0.3.0)
于 2020-04-15 创建