ggplot2标签以图标的形式出现

时间:2014-03-15 21:55:50

标签: r ggplot2 label

我有一个使用jason.bryer(see)的包形式的Likert-scale制作的ggplot。如果您使用原始数据运行代码 here,然后我的极端标签(在最右边)不再出现​​在图表中(见下图)。我该如何解决?

enter image description here

我使用的代码:

library(ggplot2)
library(likert)
library(gridExtra)
competence_bachelor <- rawdata[, substr(names(rawdata), 1, 4) == "Q002"]
competence_bachelor <- rename(competence_bachelor, c(Q002_01 = "Ability to propose new ideas and new solutions", Q002_02 = "Ability to present in public", Q002_03 = "Ability to use a computer", Q002_04 = "Ability to use the Internet", Q002_05 = "Ability to use statistical programs", Q002_06 = "Ability to write reports", Q002_07 = "Knowledge of economic concepts", Q002_08 = "Knowledge of legal concepts", Q002_09 = "Ability to understand the other's point of view", Q002_10 = "Ability to rapidly acquire new knowledge", Q002_11 = "Ability to team work", Q002_12 = "Ability to do analysis with quantitative methods", Q002_13 = "Ability to do analysis with qualitative methods", Q002_14 = "Knowledge of English", Q002_15 = "Knowledge of another foreign language"))
i <- 1
while(i<=ncol(competence_bachelor)) {
  competence_bachelor[[i]] = factor(competence_bachelor[[i]],labels = c("insignificant", "2", "3", "4", "5", "6", "7", "8", "9", "very relevant"), levels=c(1:10))
  i <- i + 1
}
competence_bachelor_plot <- likert(competence_bachelor)
p <- plot(competence_bachelor_plot, centered = FALSE, include.histogram = FALSE) + ggtitle("How do you rate your skills gained with the Bachelor's?*") + theme(axis.text.y = element_text(colour = "black"), axis.text.x = element_text(colour = "black"))
g <- arrangeGrob(p, sub = textGrob("*Order of questions was randomized and only extremes labeled in online questionaire.", x = 0, hjust = -0.1, vjust=0.1, gp = gpar(fontface = "italic", fontsize = 10)))
print(p)
ggsave((filename="competence_bachelor.pdf"), scale = 1, width = par("din")[1], height = par("din")[2], units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, g)

3 个答案:

答案 0 :(得分:5)

首先,虽然图例中最右边的元素没有显示,但它们可以在pdf中正确呈现。

问题是图例太长了,所以会被剪裁。当然,一种选择是使显示窗口更大。另一个是使传奇变小。您可以通过在p:

定义的末尾添加一行来实现
p <- plot(competence_bachelor_plot, centered = FALSE, include.histogram = FALSE) + 
  ggtitle("How do you rate your skills gained with the Bachelor's?*") + 
  theme(axis.text.y = element_text(colour = "black"), axis.text.x = element_text(colour = "black"))+
  theme(legend.key.size=unit(.01,"npc"))

您也可以删除名称(&#34;响应&#34;),因为它是多余的并且会扰乱对称性。这也允许你使图例本身更大。

p <- plot(competence_bachelor_plot, centered = FALSE, include.histogram = FALSE) + 
  ggtitle("How do you rate your skills gained with the Bachelor's?*") + 
  theme(axis.text.y = element_text(colour = "black"), axis.text.x = element_text(colour = "black"))+
  theme(legend.key.size=unit(0.02,"npc"))+guides(fill=guide_legend(""))

答案 1 :(得分:2)

p <- plot(competence_bachelor_plot,
          centered = FALSE,
          include.histogram = FALSE) + 
  ggtitle("How do you rate your skills gained with the Bachelor's?*") + 
  theme(axis.text.y = element_text(colour = "black"), 
        axis.text.x = element_text(colour = "black"),
        plot.margin = unit(c(2, 2, 2, 2), "cm"))

enter image description here

答案 2 :(得分:2)

您还可以将图例设为两行,这样可以创建充足的空间:

指南(fill = guide_legend(nrow = 2))