ggplot2-图例篇
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot2-图例篇相关的知识,希望对你有一定的参考价值。
参考技术A ggplot2会自动生成图例,我们可以对图例进行删改。方法一:用 guides(fill=FALSE) 隐藏由fill产生的图例,同理可以隐藏由color、shape产生的图例。
方法二:增加图层的时候把参数 show_guide 设为 FALSE,隐藏某图层的图例。
方法三:作用和方法一类似,设置 scale_fill_discrete(guide = FALSE),可以把中间的 fill 改成 color 或者 shape,从而隐藏相应的图例。
方法三:用 theme(legend.position = “none”) 来隐藏全局图例。
用 scale_color_discrete,通过 breaks 参数设置图例的顺序(注意也可以把color改成fill或者shape):
直接将图例逆序:
p + guides(color = guide_legend(reverse=TRUE))
或者:p + scale_color_discrete(guide = guide_legend(reverse=TRUE))
或者:p + scale_color_discrete(breaks = rev(levels(dt$C)))
不显示图例,theme(legend.position = “none”)
第一种方法属于theme主题系统,元素名legend.position,引号内可选参数有right、left、bottom,对应右、下、左
第二种方法,元素legend.position和legend.justification同时出现并且值相同,想象图例在坐标轴里,则c(0, 1)为面板左上角,c(1, 0)为面板右下角,c(0.5, 0,5)为面板中间;面板指绘图区域。
参考:https://blog.csdn.net/Bone_ACE/article/details/47284805
ggplot 有两个图例,颜色图例中显示错误的形状
【中文标题】ggplot 有两个图例,颜色图例中显示错误的形状【英文标题】:ggplot has two legends and the wrong shape shows up in the color legend 【发布时间】:2016-12-29 15:07:51 【问题描述】:我正在使用 R 中的 ggplot 创建图表,但图例未正确显示。首先,我得到了两个图例,一个用于颜色,一个用于线型。尽管我在 scale_color_manual 和 scale_linetype_manual 中放置了相同的项目,但这些项目都出现了,正如关于该主题的其他几篇文章中所建议的那样。此外,颜色图例为三个项目中的每一个都显示相同的形状(带点和 x 的线),而它们应该都不同(前两个应该是带点的线,而第三个应该是 x没有线)。
这是一个可重现的例子。
library(ggplot2)
library(dplyr)
#specify color palette
b.navHexRGB <- c(green=rgb(149,214,0, maxColorValue=255),
red=rgb(229,60,46, maxColorValue=255),
gray=rgb(85,87,89, maxColorValue=255),
dark_green=rgb(100,140,26, maxColorValue=255),
yellow=rgb(255,183,24, maxColorValue=255),
purple=rgb(139,24,155, maxColorValue=255),
blue=rgb(0,147,201, maxColorValue = 255))
#create plot
ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
size = .9) +
geom_point(aes(group = treatment, colour=treatment),
size = 1.5) +
geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
aes(y=stat_sig, colour=treatment),
size = 2.5,
shape=4,
na.rm=T) +
guides(colour=guide_legend(nrow=3)) +
scale_color_manual(name= "Variable",values=c(palette(b.navHexRGB)), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
scale_linetype_manual(name="Variable",values=c(1,2), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
ylab("Average Daily Consumption (kWh)") +
xlab("Year-Month") +
theme_bw() +
theme(legend.title = element_blank(),
legend.justification = c(0,0),
legend.position = "bottom",
legend.key = element_rect(fill = "white",colour = "white"),
#legend.key.width = unit(1.1, "cm"),
axis.text.x = element_text(angle=45, hjust=1, color="black"),
axis.text.y = element_text(color="black"),
axis.title.y = element_text(vjust=1)
)
数据
df <- structure(list(treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), .Label = c("Control", "Recipient"), class = "factor"),
bill_month = c(9, 9, 10, 10, 11, 11, 12, 12, 1, 1, 2, 2,
3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8), bill_year = c(2013,
2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014), bill_yrmo = c(201309, 201309, 201310,
201310, 201311, 201311, 201312, 201312, 201401, 201401, 201402,
201402, 201403, 201403, 201404, 201404, 201405, 201405, 201406,
201406, 201407, 201407, 201408, 201408), mean_kwh = c(34.1891698781763,
34.8263665605318, 22.998584869823, 23.6329516672246, 21.0428206185862,
21.7774153609304, 25.4992975653725, 25.8397296039854, 28.74368522348,
29.200670842288, 29.8474912589325, 30.373483172434, 26.7411627390396,
26.4600472396878, 21.628265542195, 21.3047667878863, 19.502019234349,
19.062337524723, 24.1381516068859, 24.3165665754673, 27.8915927136898,
28.3625761820341, 26.8570348685593, 27.1359185596385), p.value = c(9.36594553258583e-07,
9.36594553258583e-07, 1.76373182797948e-13, 1.76373182797948e-13,
2.12425701682086e-15, 2.12425701682086e-15, 0.00415203493379312,
0.00415203493379312, 0.00109178463449181, 0.00109178463449181,
0.00122110380638705, 0.00122110380638705, 0.0438138636035026,
0.0438138636035026, 0.00140538140516743, 0.00140538140516743,
5.74367939388898e-07, 5.74367939388898e-07, 0.100848768452669,
0.100848768452669, 0.000172505914392074, 0.000172505914392074,
0.145110211153141, 0.145110211153141), stat_sig = c(19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, NA, NA, 19, 19, NA, NA)), .Names = c("treatment", "bill_month",
"bill_year", "bill_yrmo", "mean_kwh", "p.value", "stat_sig"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -24L))
【问题讨论】:
只是一个指定的调色板。 【参考方案1】:我遇到了类似的问题。在 scale_color_manual()、scale_shape_manual() 和 scale_linetype_manual() 中指定 name="Variable" 解决了这个问题。 values 参数的命名向量并没有改变我的结果。
【讨论】:
【参考方案2】:谢谢大家。我将上面的两个响应结合起来得到了我需要的东西。
ggplot(data = avgkwh_pre2, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
geom_point(aes(group = treatment, colour = treatment, shape = treatment),
size = 2) +
geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
size = .9) +
scale_shape_manual(values = c("Recipient" = 16, "Control" = 16, "Indicates the difference is statistically significant" = 4)) +
scale_color_manual(values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]),
breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
scale_linetype_manual(values = c("Recipient" = 1,"Control" = 2, "Indicates the difference is statistically significant" = 0),
breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
ylab("Average Daily Consumption (kWh)") +
xlab("Year-Month") +
ggtitle(paste("Group Starting", rct_start)) +
theme_bw() +
theme(legend.title = element_blank(),
legend.justification = c(0,0),
legend.position = "bottom",
legend.key = element_rect(fill = "white",colour = "white"),
#legend.key.width = unit(1.1, "cm"),
axis.text.x = element_text(angle=45, hjust=1, color="black"),
axis.text.y = element_text(color="black"),
axis.title.y = element_text(vjust=1)
)
【讨论】:
【参考方案3】:我经常发现在调用ggplot
之前排列数据很有用。我rbind
“统计显着”行与主数据框对齐,并将“统计显着”行的“y”美学对齐到与其他数据相同 (mean_kwh = stat_sig
):
dd <- rbind(df, df %>%
mutate(treatment= 'Indicates the difference is statistically significant',
mean_kwh = stat_sig))
然后拨打ggplot
。注意“统计显着”也有linetype
,只是它是0
:
#create plot
ggplot(data = dd, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
geom_point(aes(group = treatment, colour = treatment, shape = treatment),
size = 1.5) +
geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
size = .9) +
scale_shape_manual(values = c(1, 2, 4)) +
scale_color_manual(values = c(palette(b.navHexRGB)),
breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
scale_linetype_manual(values = c(1, 2, 0),
breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
labs(y = "Average Daily Consumption (kWh)",
x = "Year-Month") +
theme_bw() +
theme(legend.title = element_blank(),
legend.justification = c(0,0),
legend.position = "bottom",
legend.key = element_rect(fill = "white",colour = "white"),
axis.text.x = element_text(angle=45, hjust=1, color="black"),
axis.text.y = element_text(color="black"),
axis.title.y = element_text(vjust=1)
)
输出:
【讨论】:
【参考方案4】:如果情节的一般美学映射在主要的ggplot
调用中处理,则可以简化您对geom_line
和geom_point
的调用。但主要问题是您在 scale
调用中指定颜色和线型的方式。如果您向values
参数提供命名向量,这些函数更不容易出错,这保证了对映射的控制。
这段代码:
ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh, color = treatment, lty = treatment)) +
geom_line(size = .9, aes(group = treatment)) +
geom_point(size = 1.5) +
geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
aes(y=stat_sig, colour=treatment),
size = 2.5,
shape=4,
na.rm=T) +
scale_color_manual(name = "Variable", values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
scale_linetype_manual(name="Variable",values = c("Recipient" = 2, "Control" = 1, "Indicates the difference is statistically significant" = 0), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
labs(x = "Year-Month", y = "Average Daily Consumption (kWh)") +
theme_bw() +
theme(legend.title = element_blank(),
legend.justification = c(0,0),
legend.position = "bottom",
legend.key = element_rect(fill = "white",colour = "white"),
legend.direction = "vertical",
axis.text.x = element_text(angle=45, hjust=1, color="black"),
axis.text.y = element_text(color="black"),
axis.title.y = element_text(vjust=1)
)
产生这个情节:
【讨论】:
以上是关于ggplot2-图例篇的主要内容,如果未能解决你的问题,请参考以下文章
R语言ggplot2可视化:在ggplot2中将图例(legend)移到图内自定义图例所处的位置自定义图例背景图例所处边框的颜色