R 按带正负条的值的数量标记条形图
我有一个barplot stat=identity,并希望用每个条的值的数量N来标记这些条。我对正负条有问题。一种解决方案是将正极条的标签做成白色,以便将其写在顶部R 按带正负条的值的数量标记条形图,r,ggplot2,bar-chart,geom-text,R,Ggplot2,Bar Chart,Geom Text,我有一个barplot stat=identity,并希望用每个条的值的数量N来标记这些条。我对正负条有问题。一种解决方案是将正极条的标签做成白色,以便将其写在顶部 ggplot(AP_Group, aes(Labels, Mean))+ geom_bar(stat = "identity") + theme_minimal() + ggtitle(expression(paste("Air Pressure - C_PM"[1], " - All Season"))) +
ggplot(AP_Group, aes(Labels, Mean))+
geom_bar(stat = "identity") +
theme_minimal() +
ggtitle(expression(paste("Air Pressure - C_PM"[1], " - All Season"))) +
xlab("Air Pressure [hPa]") +
ylab("Shap Value") +
geom_text(aes(label=N), color="black", size=3.5, vjust = 1.8) +
theme(plot.title = element_text(color="black", size=14, margin = margin(t = 0, r = 0, b = 15, l = 0)),
axis.title.x = element_text(color="black", size=14, margin = margin(t = 15, r = 0, b = 0, l = 0)),
axis.title.y = element_text(color="black", size=14, margin = margin(t = 0, r = 15, b = 0, l = 0))) +
theme(plot.title = element_text(hjust=0.5))
一种方法是让V4成为这样的美学
df <- tibble(x = c(1, 2), y = c(-1, 2)) #sample data
df %>% ggplot(aes(x=x, y=y)) +
geom_bar(stat = "identity") +
geom_text(aes(label = y, vjust = -sign(y)))
一种方法是让V4成为这样的美学
df <- tibble(x = c(1, 2), y = c(-1, 2)) #sample data
df %>% ggplot(aes(x=x, y=y)) +
geom_bar(stat = "identity") +
geom_text(aes(label = y, vjust = -sign(y)))
如果您真的想有条件地将其按符号上色,这里有一个可能的解决方案:
# fake df
df <- data.frame(Mean = c(1,3,-5),Labels = c("a","b","c"))
# here you decide to put white or black conditionally
df$color <- ifelse(df$Mean > 0, 'white','black')
library(ggplot2)
ggplot(df, aes(Labels, Mean))+
geom_bar(stat = "identity") +
theme_minimal() +
ggtitle(expression(paste("Air Pressure - C_PM", " - All Season"))) +
xlab("Air Pressure [hPa]") +
ylab("Shap Value") +
# here in aes() you put the color made
geom_text(aes(label=Mean, color=color), size=3.5, vjust = 1.8) +
# here you define the colors (it means "white" could be the color you want)
scale_color_manual(values = c("black" = "black", "white" = "white"))+
# you can remove the useless legend
theme(legend.position="none")
如果您真的想有条件地将其按符号上色,这里有一个可能的解决方案:
# fake df
df <- data.frame(Mean = c(1,3,-5),Labels = c("a","b","c"))
# here you decide to put white or black conditionally
df$color <- ifelse(df$Mean > 0, 'white','black')
library(ggplot2)
ggplot(df, aes(Labels, Mean))+
geom_bar(stat = "identity") +
theme_minimal() +
ggtitle(expression(paste("Air Pressure - C_PM", " - All Season"))) +
xlab("Air Pressure [hPa]") +
ylab("Shap Value") +
# here in aes() you put the color made
geom_text(aes(label=Mean, color=color), size=3.5, vjust = 1.8) +
# here you define the colors (it means "white" could be the color you want)
scale_color_manual(values = c("black" = "black", "white" = "white"))+
# you can remove the useless legend
theme(legend.position="none")
这是我将如何做+1。这是我将如何做+1。