如何按填充颜色对 ggplot2 中的条形进行分组,同时保持降序?
How can I group my bars in ggplot2 by fill color, while maintaining descending order?
我目前正在绘制一个条形图,其中我从 timeA 和 timeB 收集了数据。我对不同的物体进行了测量(例如:质量)。时间A组和时间B组测量对象不一致
- 我希望条形图按降序排列
- 但我还希望条形图按时间分组,即 fill=time
我该如何实现?
数据集示例:
df$measures <- c(2,4,26,10,18,20,14,22,12,16,24,6,8,28)
df$object <- seq.int(nrow(df))
df$time<- "timea"
df[6:14, 3] = "time"
这是目前我的图表的粗略代码
plot <-ggplot(df, aes(x=reorder(object, -measures), y=measures, fill=time))+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5))+
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"), labels=c("timea", "timeb"))
plot
我附上了一张图片,以防图表大致看起来像什么。理想情况下,我希望一侧的所有蓝色条按降序排列,另一侧的所有红色条按降序排列;都在同一个地块。
如果您想要同一个图表中的所有内容,您可以使用数据框中的因子水平。
library(dplyr)
library(ggplot2)
df %>%
arrange(time, desc(measures)) %>%
mutate(object = factor(object, object)) %>%
ggplot() + aes(x=object, y=measures, fill=time)+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
plot.title = element_text(hjust = 0.5))+
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"),
labels=c("timea", "timeb"))
您也可以在这里使用分面 -
ggplot(df) + aes(x=reorder(object, -measures), y=measures, fill=time)+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
plot.title = element_text(hjust = 0.5))+
facet_wrap(.~time, scales = 'free') +
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"),
labels=c("timea", "timeb"))
数据
df <- tibble::tibble(measures = c(2,4,26,10,18,20,14,22,12,16,24,6,8,28),
object = seq_along(measures),
time = sample(c("timea", "timeb"), length(measures), replace = TRUE))
我目前正在绘制一个条形图,其中我从 timeA 和 timeB 收集了数据。我对不同的物体进行了测量(例如:质量)。时间A组和时间B组测量对象不一致
- 我希望条形图按降序排列
- 但我还希望条形图按时间分组,即 fill=time
我该如何实现?
数据集示例:
df$measures <- c(2,4,26,10,18,20,14,22,12,16,24,6,8,28)
df$object <- seq.int(nrow(df))
df$time<- "timea"
df[6:14, 3] = "time"
这是目前我的图表的粗略代码
plot <-ggplot(df, aes(x=reorder(object, -measures), y=measures, fill=time))+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5))+
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"), labels=c("timea", "timeb"))
plot
我附上了一张图片,以防图表大致看起来像什么。理想情况下,我希望一侧的所有蓝色条按降序排列,另一侧的所有红色条按降序排列;都在同一个地块。
如果您想要同一个图表中的所有内容,您可以使用数据框中的因子水平。
library(dplyr)
library(ggplot2)
df %>%
arrange(time, desc(measures)) %>%
mutate(object = factor(object, object)) %>%
ggplot() + aes(x=object, y=measures, fill=time)+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
plot.title = element_text(hjust = 0.5))+
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"),
labels=c("timea", "timeb"))
您也可以在这里使用分面 -
ggplot(df) + aes(x=reorder(object, -measures), y=measures, fill=time)+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
plot.title = element_text(hjust = 0.5))+
facet_wrap(.~time, scales = 'free') +
scale_fill_manual(name="Legend", values = c("firebrick", "cornflowerblue"),
labels=c("timea", "timeb"))
数据
df <- tibble::tibble(measures = c(2,4,26,10,18,20,14,22,12,16,24,6,8,28),
object = seq_along(measures),
time = sample(c("timea", "timeb"), length(measures), replace = TRUE))