英文:
Order columns in plot
问题
我想用geom_col()创建一个堆积图,使用以下数据集。
tmp.df <- data.frame(
original = c(2.68, 1.05, 72.89, 11.81, 11.67, 2.85, 1.11, 72.74, 12.05, 11.95),
sensitivity = c('Si', 'Si', 'Si', 'Si', 'Si', 'STi', 'STi', 'STi', 'STi', 'STi'),
parameters = c('OMd', 'ED6_N', 'CP', 'dr_N', 'GE', 'OMd', 'ED6_N', 'CP', 'dr_N', 'GE')
)
因此,我像下面这样做了:
ord.eng <- c("CP", "GE", "OMd", "ED6_N", "dr_N")
library(ggplot2)
ggplot(tmp.df, aes(x = sensitivity, y = original)) +
geom_col(aes(fill = parameters), width = 0.5, color = "black") +
ggtitle("Nu/Total N excretion") +
xlab("Sobol index") +
ylab("Sobol indices (%)") +
theme_classic() +
theme(
plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
axis.text.x = element_text(color = "black", size = 18),
axis.text.y = element_text(color = "black", size = 18),
axis.title = element_text(face = "bold", size = 18, color = "black"),
legend.text = element_text(size = 15),
legend.title = element_text(hjust = 0.5, size = 15)
) +
scale_y_continuous(breaks = seq(0, 140, 20)) +
scale_fill_manual(
name = "Variables",
limits = ord.eng,
values = c(
"CP" = "White",
"GE" = "ivory",
"OMd" = "ivory3",
"ED6_N" = "gray",
"dr_N" = "black"
)
)
然而,柱状图没有按照'ord.eng'的顺序堆叠。它只改变了图例的顺序。但我希望它与图例的顺序相同。您有什么好主意吗?
谢谢你的帮助。
英文:
I'd like to make a stocked graph with geom_col() with below dataset.
tmp.df <- data.frame(
original = c(2.68, 1.05, 72.89, 11.81, 11.67, 2.85, 1.11, 72.74, 12.05, 11.95),
sensitivity = c('Si', 'Si', 'Si', 'Si', 'Si', 'STi', 'STi', 'STi', 'STi', 'STi'),
parameters = c('OMd', 'ED6_N', 'CP', 'dr_N', 'GE', 'OMd', 'ED6_N', 'CP', 'dr_N', 'GE')
)
Thus I did like below:
ord.eng <- c("CP", "GE", "OMd", "ED6_N", "dr_N")
library(ggplot2)
ggplot(tmp.df, aes(x = sensitivity, y = original))+
geom_col(aes(fill = parameters), width = 0.5, color = "black")+
ggtitle("Nu/Total N excretion") +
xlab("Sobol index")+
ylab("Sobol indices (%)")+
theme_classic()+
theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
axis.text.x = element_text(color = "black", size = 18),
axis.text.y = element_text(color = "black", size = 18),
axis.title = element_text(face = "bold", size = 18, color = "black"),
legend.text = element_text(size = 15),
legend.title = element_text(hjust = 0.5, size = 15))+
scale_y_continuous(breaks = seq(0, 140, 20))+
scale_fill_manual(name = "Variables", limits = ord.eng,
values = c("CP" = "White", "GE" = "ivory", "OMd" = "ivory3", "ED6_N" = "gray", "dr_N" = "black"))
However, the bar chart didn't stock 'ord.eng' order.
It changed just legend order.
But I'd like to stock it same order with legend.
Do you have any good ideas?
Thank you for your help.
答案1
得分: 0
将要排序的列转换为因子是一种做法。levels
参数告诉R变量可以具有不同的值,而ggplot
在确定如何堆叠时使用levels
的顺序:
tmp.df %>%
mutate(parameters = factor(parameters, levels = ord.eng)) %>%
ggplot(aes(x = sensitivity, y = original, fill = parameters)) +
geom_col(aes(fill = parameters), width = 0.5, color = "black") +
ggtitle(sprintf("Nu/Total N excretion")) +
xlab("Sobol index") +
ylab("Sobol indices (%)") +
theme_classic() +
theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
axis.text.x = element_text(color = "black", size = 18),
axis.text.y = element_text(color = "black", size = 18),
axis.title = element_text(face = "bold", size = 18, color = "black"),
legend.text = element_text(size = 15),
legend.title = element_text(hjust = 0.5, size = 15)) +
scale_y_continuous(breaks = seq(0, 140, 20)) +
scale_fill_manual(name = "Variables", limits = ord.eng,
values = c("CP" = "White", "GE" = "ivory", "OMd" = "ivory3", "ED6_N" = "gray", "dr_N" = "black"))
英文:
One way of doing this is to turn the column you want to order by into a factor. The levels
argument tells R the different values the variable can have, and ggplot
uses the order of levels
when deciding how to stack:
tmp.df %>%
mutate(parameters = factor(parameters, levels = ord.eng)) %>% ### changed line ###
ggplot(aes(x = sensitivity, y = original, fill = parameters)) +
geom_col(aes(fill = parameters), width = 0.5, color = "black")+
ggtitle(sprintf("Nu/Total N excretion"))+
xlab("Sobol index")+
ylab("Sobol indices (%)")+
theme_classic()+
theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
axis.text.x = element_text(color = "black", size = 18),
axis.text.y = element_text(color = "black", size = 18),
axis.title = element_text(face = "bold", size = 18, color = "black"),
legend.text = element_text(size = 15),
legend.title = element_text(hjust = 0.5, size = 15))+
scale_y_continuous(breaks = seq(0, 140, 20))+
scale_fill_manual(name = "Variables", limits = ord.eng,
values = c("CP" = "White", "GE" = "ivory", "OMd" = "ivory3", "ED6_N" = "gray", "dr_N" = "black"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论