首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R- ggplot labs(subtitle=...和caption=...)不工作

R- ggplot labs(subtitle=...和caption=...)不工作
EN

Stack Overflow用户
提问于 2018-11-24 00:26:28
回答 1查看 4.8K关注 0票数 0

在将文本元素正确添加到某些图表中的绘图时,我遇到了问题。我使用ggplot2和+ labs(...)但是只有一些元素包含在我的图中。

这是其中一个图表:

代码语言:javascript
运行
复制
colnames(dane.trust)[1] = "Country" #GEO.INDIC_WB
w5 <- ggplot(dane.trust, aes(x = reorder(Country, Trust.Index), y = Trust.Index, fill=Country)) + scale_fill_brewer(palette = "Set2")
w5 + geom_bar(stat="identity") + #guides(fill=FALSE)  +
geom_text(data=dane.trust[,c(1,6)], label = round(Trust.Index, digits = 2),vjust = -0.5, aes(inherit.aes = TRUE, fontface=2)) +
theme_bw() + labs(x="", y="Average Rating (0-10)", title = "Overall trust levels", subtitle="Index of trust to political and legal systems, police and others") +
theme(title = element_text(face = "bold", color = "black"), axis.title = element_text(face = "bold", color = "black"), panel.grid.major.x = element_blank())

Title、x.label和y.label运行得很好,但我看不到字幕和标题。

没有错误,只是那些元素不存在。

有没有人知道问题出在哪里?

EDIT1:这是> dput(dane.trust)的输出

代码语言:javascript
运行
复制
structure(list(Country = structure(1:5, .Label = c("Bulgaria", 
"Hungary", "Poland", "Romania", "Slovakia"), class = "factor"), 
Trust.in.the.political.system = c(2.6, 4.5, 3.5, 4.8, 3.5
), Trust.in.the.police = c(3.6, 5.7, 5.2, 6.4, 4.4), Trust.in.others = c(4.2, 
5.3, 6, 6.4, 5.8), Trust.in.the.legal.system = c(3, 5.1, 
4.2, 5.8, 3.6), Trust.Index = c(3.35, 5.15, 4.725, 5.85, 
4.325)), .Names = c("Country", "Trust.in.the.political.system", 
"Trust.in.the.police", "Trust.in.others", "Trust.in.the.legal.system", 
"Trust.Index"), row.names = c(NA, -5L), class = "data.frame")

EDIT2:我刚刚在另一台计算机上检查了我的原始代码,它工作了。是什么导致了这种情况?

好了,在重新安装R和RStudio之后,一切都正常了,感谢大家的回答:)。

EN

回答 1

Stack Overflow用户

发布于 2018-11-24 03:38:51

这样就可以了

代码语言:javascript
运行
复制
w5 <- ggplot(dane.trust, aes(x = reorder(Country, Trust.Index), y = Trust.Index, fill=Country)) + scale_fill_brewer(palette = "Set2")
w5 + geom_bar(stat="identity") + #guides(fill=FALSE)  +
  geom_text(vjust = -0.5, aes(fontface=2), label = dane.trust$Trust.Index) +
  theme_bw() + labs(x="", y="Average Rating (0-10)", title = "Overall trust levels", subtitle="Index of trust to political and legal systems, police and others") +
  theme(title = element_text(face = "bold", color = "black"), axis.title = element_text(face = "bold", color = "black"), panel.grid.major.x = element_blank())

如果提供了Trust.Index,则geom_text()中的参数label只能计算aes()内的data。您可以在aes()外部的label = ...中指定向量,也可以在aes()内部插入label =...,如下所示:geom_text(data = dane.trust[, c(1, 6)], ..., aes(label = Trust.Index))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53450097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档