在将某些文本元素正确添加到图表中时出现问题。我使用ggplot2和+ labs(...),但我的绘图中仅包含一些元素。
这是图表之一:
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())
标题,x.label和y.label工作正常,但我既看不到字幕,也看不到字幕。
没有错误,只是那些元素不存在。
有人知道可能是什么问题吗?
EDIT1: 这是> dput(dane.trust)
的输出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后,一切工作顺利,谢谢大家的回答:)。
答案 0 :(得分:0)
这应该做
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())
提供label
时,geom_text()
中的参数aes()
只能对data
内的Trust.Index求值。您可以在label = ...
之外的aes()
中指定向量,也可以像这样label =...
aes()
内插入geom_text(data = dane.trust[, c(1, 6)], ..., aes(label = Trust.Index))
答案 1 :(得分:0)
您的错误发生在对geom_text()
fixit的错误调用中,其他一切都很好显示了。
library(ggplot2)
dane.trust <- 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")
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") +
geom_text(aes(label = round(Trust.Index, digits = 2)), vjust = -0.5) +
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()
)
由reprex package(v0.2.1)于2018-11-23创建