更改 facet wrap ggplot 中的标签 [重复]
Posted
技术标签:
【中文标题】更改 facet wrap ggplot 中的标签 [重复]【英文标题】:Change labels in facet wrap ggplot [duplicate] 【发布时间】:2018-09-24 12:57:43 【问题描述】:问候,
我在使用 facet_wrap 正确命名 ggplot2 中的标签时遇到了一些困难。我的绘图代码是:
library(ggplot2)
mlm.plots <- ggplot(positions.df, aes(x, y)) +
geom_point(size=0.75, shape=1, colour="darkred", fill="pink")+
geom_abline(data = multilevel.df, aes(intercept=V2, slope=V3,
group=party), color="red", size=.1)+
facet_wrap(~party, ncol=2) +
xlab("")+
ylab("")
这会产生以下内容:
我真正想要的不是标签(“1”,“2”...),而是在每种情况下都有政党的名称。我觉得应该有一个使用 labeller() 的优雅解决方案,但我想不出一个。
我认为部分问题在于我将派对设置为以下因素:
party <- as.factor(rep(c("National Coalition Party", "Centre Party",
"Social Democratic Party", "Left Alliance", "Christian Democrats",
"True Finns", "Swedish People's Party", "Greens"), J))
我也知道,如果派对是 as.character,情节就会变得非常不稳定。
制作图表的数据如下:
structure(list(party = c(5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4,
2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5,
1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8,
7, 3, 5, 1, 6, 4, 2, 8, 7, 3), x = c(-16.667, -36.735, 43.243,
-64.286, 37.963, -9.091, 6.593, -38.614, -30.496, -10.549, -45.455,
-46.515, 25.926, -23.81, -29.53, -38.614, -4.478, 1.266, 1.176,
-30.357, 5, 13.084, -7.692, -38.614, 14.62, 26.829, -13.725,
-14.894, 24.299, 13.084, 4.145, -13.433, 29.464, -1.049, -1.37,
-38.168, 19.444, -5.882, 14.516, -6.25, 9.756, -7.636, -24.742,
-45.946, 7.813, -5.882, -19.931, -33.523, -20.556, -15.09, -36.432,
-42.051, -15.108, -6.518, -25.472, -21.471, 13.75, -16.383, -11.384,
-44.767, -16.771, 0.472, -23.392, -27.715), y = c(-0.295492376,
0.187137648, -0.209073538, 1.026732887, -0.524148543, 0.232093035,
-1.617201837, -0.038851011, -0.351777544, 0.637192933, -0.783167803,
1.549387151, -0.742792721, -0.054633476, -2.204811412, 0.009461977,
-0.594714182, 1.172333694, -0.951553793, 1.59911439, -1.246200649,
-0.337551454, -2.631499836, 0.6051641, -0.885991535, 1.492537342,
-1.275241929, 1.658246706, -1.331133971, -0.676627085, -3.220241861,
0.82922329, -0.841711554, 1.611623219, -1.531110402, 1.469424694,
-1.979679497, -0.724442893, -3.523278033, 1.187782421, -0.842631246,
1.35252299, -1.950335, 0.859798616, -2.152810527, -0.623310324,
-3.48956421, 1.718330701, -0.809637545, 0.741273409, -1.96458669,
1.466255347, -2.675088542, -1.066556748, -3.436585287, 1.935368096,
-0.870188157, 0.477034948, -2.49292584, 1.93375064, -2.924310472,
-1.033098158, -3.250669464, 2.086336567)), .Names = c("party",
"x", "y"), row.names = c(NA, -64L), class = "data.frame")
和
structure(list(party = c(5, 1, 6, 4, 2, 8, 7, 3), V2 =
c(-0.671389852256272,
1.07302815113772, -1.26372215643281, 1.79721076947721,
-1.95951156748975,
-0.541929683566524, -2.8106689095983, 1.42500879635995), V3 =
c(-0.000574845695491941,
0.018171274525851, 0.0127869327689727, 0.00934727979573554,
0.0251920546515927,
0.00326951650086729, 0.00867962541673107, 0.0153496027643832),
V4 = c(-0.417933984027918, -0.417933984027918, -0.417933984027918,
-0.417933984027918, -0.417933984027918, -0.417933984027918,
-0.417933984027918, -0.417933984027918), V5 = c(0.0114033982479481,
0.0114033982479481, 0.0114033982479481, 0.0114033982479481,
0.0114033982479481, 0.0114033982479481, 0.0114033982479481,
0.0114033982479481)), .Names = c("party", "V2", "V3", "V4",
"V5"), row.names = c("National.Coalition.Party", "Centre.Party",
"Social.Democratic.Party", "Left.Alliance", "Christian.Democrats",
"True.Finns", "Swedish.People.s.Party", "Greens"), class =
"data.frame")
【问题讨论】:
【参考方案1】:将party
列转换为因子时,使用labels
参数指定标签。
positions.df$party <- factor(positions.df$party,
labels = c("National Coalition Party", "Centre Party",
"Social Democratic Party", "Left Alliance", "Christian Democrats",
"True Finns", "Swedish People's Party", "Greens"))
multilevel.df$party <- factor(multilevel.df$party,
labels = c("National Coalition Party", "Centre Party",
"Social Democratic Party", "Left Alliance", "Christian Democrats",
"True Finns", "Swedish People's Party", "Greens"))
之后,您可以使用原始代码绘制数据。
mlm.plots <- ggplot(positions.df, aes(x, y)) +
geom_point(size=0.75, shape=1, colour="darkred", fill="pink")+
geom_abline(data = multilevel.df, aes(intercept=V2, slope=V3,
group=party), color="red", size=.1)+
facet_wrap(~party, ncol=2) +
xlab("")+
ylab("")
mlm.plots
【讨论】:
以上是关于更改 facet wrap ggplot 中的标签 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 ggplot/ggplotly 中使用 facet_wrap 时如何防止 y 轴挤压标签?
在 R 中使用带有 facet_wrap 的 ggplot2 显示多个轴标签
R语言ggplot2可视化删除所有分面图(facet_wrap可视化的facet结果)的标签实战(Remove facet_wrap labels)
如何在 ggplot facet wrap 标签中使用不同的字体大小?
R语言ggplot2可视化分面图(faceting): ggplot2可视化分面图(facet_wrap)并设置不同的分面使用不同的坐标轴数值范围以及不同的轴标签断点间隔breaks