如何在ggplot中发现中间条形图
How to spot the mediane barplot in ggplot
我想通过两种方式找出与中值相对应的中心:
首先通过一条水平线经过中值中心(由于我有偶数个中心,我决定每次选择中心对应的两个中值中的最大值,我在这种情况下Centre 6
)
其次通过突出显示中值条形图的颜色。
我的代码:
Centre <- paste("Centre", 1:10, sep= " ")
Value <- c(10, 23, 27, 50, 60, 71, 88, 102, 110, 113)
mydata<-data.frame(Centre, Value)
ggplot(mydata, aes(x=Centre, y=Value) +
geom_col() +
coord_flip()
试试这个解决方案:
library(ggplot2)
library(dplyr)
Centre <- paste("Centre", 1:10, sep= " ")
Value <- c(10, 23, 27, 50, 60, 71, 88, 102, 110, 113)
mydata<-data.frame(Centre, Value)
mydata <- mydata %>%
mutate(new_color = ifelse(Value == 71, "yes", "no"))
ggplot(mydata, aes(x=Centre, y=Value, fill = new_color)) +
geom_col() +
coord_flip() +
scale_fill_manual(values = c("yes"="black", "no"="gray"), guide = "none") +
geom_segment(aes(x = 7, y = 0, xend = 7, yend = 71), color="red", size=2)
结果:
我想通过两种方式找出与中值相对应的中心:
首先通过一条水平线经过中值中心(由于我有偶数个中心,我决定每次选择中心对应的两个中值中的最大值,我在这种情况下Centre 6
)
其次通过突出显示中值条形图的颜色。
我的代码:
Centre <- paste("Centre", 1:10, sep= " ")
Value <- c(10, 23, 27, 50, 60, 71, 88, 102, 110, 113)
mydata<-data.frame(Centre, Value)
ggplot(mydata, aes(x=Centre, y=Value) +
geom_col() +
coord_flip()
试试这个解决方案:
library(ggplot2)
library(dplyr)
Centre <- paste("Centre", 1:10, sep= " ")
Value <- c(10, 23, 27, 50, 60, 71, 88, 102, 110, 113)
mydata<-data.frame(Centre, Value)
mydata <- mydata %>%
mutate(new_color = ifelse(Value == 71, "yes", "no"))
ggplot(mydata, aes(x=Centre, y=Value, fill = new_color)) +
geom_col() +
coord_flip() +
scale_fill_manual(values = c("yes"="black", "no"="gray"), guide = "none") +
geom_segment(aes(x = 7, y = 0, xend = 7, yend = 71), color="red", size=2)
结果: