如何在 R 中的条形图的每个条中放置值
How to put values in each bar of the barplot in R
我有这个 df:
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
并且我需要条形图的每个值都位于条形图的中心:
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value), size = 4, hjust = 0, vjust = 0)
谢谢!
这是你想要的吗?
library(dplyr)
library(ggplot2)
library(tidyr)
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value),
position = position_dodge(width=0.9))
我有这个 df:
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
并且我需要条形图的每个值都位于条形图的中心:
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value), size = 4, hjust = 0, vjust = 0)
谢谢!
这是你想要的吗?
library(dplyr)
library(ggplot2)
library(tidyr)
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value),
position = position_dodge(width=0.9))