如何使用ggplot2旋转和居中Y轴标题?

时间:2019-11-19 18:19:11

标签: r ggplot2

我正在尝试旋转ggplot2图的y轴标题,并将其居中于y轴。我已经能够成功旋转标题,但是即使使用hjust = 0.5命令,我也似乎无法将其定位在y轴上。旋转标题后,是否有任何想法可以使标题在y轴上居中?

dataset <- data.frame(cultivar = c('var1',
                                   'var1',
                                   'var1',
                                   'var1',
                                   'var3',
                                   'var3',
                                   'var3',
                                   'var3',
                                   'var2',
                                   'var2',
                                   'var2',
                                   'var2',
                                   'var3',
                                   'var3',
                                   'var1',
                                   'var2',
                                   'var2',
                                   'var2',
                                   'var1',
                                   'var2',
                                   'var3',
                                   'var3',
                                   'var1',
                                   'var1',
                                   'var1',
                                   'var3',
                                   'var3',
                                   'var1',
                                   'var2',
                                   'var2',
                                   'var2',
                                   'var2',
                                   'var3',
                                   'var1',
                                   'var3',
                                   'var1'),
                      rate = c(10,
                               20,
                               30,
                               40,
                               10,
                               20,
                               30,
                               40,
                               10,
                               20,
                               30,
                               40,
                               20,
                               40,
                               10,
                               10,
                               30,
                               20,
                               30,
                               40,
                               10,
                               30,
                               40,
                               20,
                               20,
                               10,
                               30,
                               30,
                               40,
                               10,
                               30,
                               20,
                               40,
                               10,
                               20,
                               40),
                      vi = c(1354,
                             1307,
                             1529,
                             1380,
                             1416,
                             1342,
                             1419,
                             1363,
                             1282,
                             1287,
                             1202,
                             1312,
                             1294,
                             1248,
                             1431,
                             1421,
                             1287,
                             1439,
                             1405,
                             1406,
                             1395,
                             1540,
                             1222,
                             1247,
                             1375,
                             1235,
                             1436,
                             1335,
                             1478,
                             1356,
                             1542,
                             1219,
                             1392,
                             1406,
                             1524,
                             1342))

用于生成图形的代码:

#Aggregate data frame
datasetMean <- aggregate.data.frame(dataset, by=list(dataset$cultivar, dataset$rate), mean)
datasetMean <- select(datasetMean, Group.1, Group.2, vi)
colnames(datasetMean) <- c("cultivar","rate","vi")

#Generate graph
ggplot(datasetMean, aes(x = cultivar, y = vi, fill=factor(rate)))+
  geom_bar(stat="identity", width = 0.6, position = "dodge", col = "black")+
  scale_fill_discrete(name = "Rate", labels = c("0X","1X","4X","8X"))+
  xlab("Cultivar") + ylab("vi")+
  ggtitle("VI Scores")+
  theme(plot.title = element_text(hjust = 0.5, size = 30),
        strip.text.x = element_text(size = 25),
        axis.text = element_text(size = 20),
        axis.title = element_text(size = 25),
        legend.key.size = unit(1.5, "cm"),
        legend.text = element_text(size = 15),
        legend.title = element_text(size = 20),
        axis.title.y = element_text(angle = 0, hjust = 0.5))

1 个答案:

答案 0 :(得分:2)

如果要垂直移动轴标题,则需要使用垂直调整,而不是垂直于图形面板垂直调整。

axis.title.y = element_text(angle = 0, vjust = 0.5)添加到您的theme()通话中 应该这样做。