订购图例值ggplot
Ordering legend values ggplot
我试图对情节的标签进行排序,但无论我做什么,它都没有改变。
这是一个虚拟数据框,因为我的数据框太大了,但我会得到相同的结果
df <- data.frame(treatment= c("C", "3", "4", "5", "10","HT"),
day1 = c(0.23, 0.16, 0.34, 0.21, 0.17,0.34),
day2=c(0.18,0.13,0.27,0.17,0.11,0.23),
day4=c(0.15,0.08,0.20,0.14,0.05,0.17))
我用这段代码绘制它
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line()+
ylab(expression(Transpiration (g/h)))+xlab(expression(Days))+ theme_dark()+ theme(text = element_text(size = 20))
在此之后的图例顺序是 10,3,4,5,C,HT。所以我试着改成C,3,4,5,10,HT
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line()+
ylab(expression(Transpiration (g/h)))+xlab(expression(Days))+ theme_dark()+ theme(text = element_text(size = 20))+
scale_fill_discrete(breaks=c("C","3","4","5","10","HT"))
但我一直得到相同的结果。有什么建议吗?还有如何将图例放入图表本身并删除图例的标题。感谢大家的帮助
如果您需要特定订单,您可以手动向体重秤提供 limits
。此外,您还使用了 fill
图例作为 colour
美学,这可能解释了为什么您的代码无法正常工作。
library(ggplot2)
library(tidyr)
library(dplyr)
df <- data.frame(treatment= c("C", "3", "4", "5", "10","HT"),
day1 = c(0.23, 0.16, 0.34, 0.21, 0.17,0.34),
day2=c(0.18,0.13,0.27,0.17,0.11,0.23),
day4=c(0.15,0.08,0.20,0.14,0.05,0.17))
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line() +
scale_colour_discrete(limits = df$treatment)
由 reprex package (v2.0.1)
于 2021 年 10 月 6 日创建
要删除标题并将图例放在面板内,您可以使用
scale_colour_discrete(limits = df$treatment, name = NULL) +
theme(legend.position = c(1, 1),
legend.justification = c(1, 1))
我试图对情节的标签进行排序,但无论我做什么,它都没有改变。
这是一个虚拟数据框,因为我的数据框太大了,但我会得到相同的结果
df <- data.frame(treatment= c("C", "3", "4", "5", "10","HT"),
day1 = c(0.23, 0.16, 0.34, 0.21, 0.17,0.34),
day2=c(0.18,0.13,0.27,0.17,0.11,0.23),
day4=c(0.15,0.08,0.20,0.14,0.05,0.17))
我用这段代码绘制它
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line()+
ylab(expression(Transpiration (g/h)))+xlab(expression(Days))+ theme_dark()+ theme(text = element_text(size = 20))
在此之后的图例顺序是 10,3,4,5,C,HT。所以我试着改成C,3,4,5,10,HT
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line()+
ylab(expression(Transpiration (g/h)))+xlab(expression(Days))+ theme_dark()+ theme(text = element_text(size = 20))+
scale_fill_discrete(breaks=c("C","3","4","5","10","HT"))
但我一直得到相同的结果。有什么建议吗?还有如何将图例放入图表本身并删除图例的标题。感谢大家的帮助
如果您需要特定订单,您可以手动向体重秤提供 limits
。此外,您还使用了 fill
图例作为 colour
美学,这可能解释了为什么您的代码无法正常工作。
library(ggplot2)
library(tidyr)
library(dplyr)
df <- data.frame(treatment= c("C", "3", "4", "5", "10","HT"),
day1 = c(0.23, 0.16, 0.34, 0.21, 0.17,0.34),
day2=c(0.18,0.13,0.27,0.17,0.11,0.23),
day4=c(0.15,0.08,0.20,0.14,0.05,0.17))
df %>%
pivot_longer(-treatment, names_to = "days", names_prefix = "day") %>%
mutate(days = as.integer(days),
treatment = as.factor(treatment)) %>%
ggplot(aes(days, value, color = treatment)) +
geom_line() +
scale_colour_discrete(limits = df$treatment)
由 reprex package (v2.0.1)
于 2021 年 10 月 6 日创建要删除标题并将图例放在面板内,您可以使用
scale_colour_discrete(limits = df$treatment, name = NULL) +
theme(legend.position = c(1, 1),
legend.justification = c(1, 1))