如何在ggplot函数中使用sprintf右对齐和右填充我的Y轴标签?

时间:2019-01-15 16:59:49

标签: r ggplot2 stringr

我有3个图,我想在这些图上粘贴我的y轴标签。我碰到了this post,并使用了该功能来填充y轴,以使所有3个图中的标签长度​​都等于60个字符的长度(或大于55的任何数字)。但是即使在阅读sprintf函数的文档后,也不确定是否正确使用了它。我一直在使用和调整的sprintf函数是:sprintf('%60s', mean_name)。当我尝试使用它并将其左对齐以查看是否存在间距问题时,它确实很时髦-请参阅底部的图片(已编辑数据)。如果我在60前面放一个负数,那末尾不填充字符串吗?

这是我的3个地块的代码。

pq1_plop <-  ggplot(pq1_agged, aes(y=mean_name, x=mean_2018)) + 
  geom_vline(xintercept = 3, size = 0.5, color = "#00C4F3") + #Benchmark static line
  geom_text(data=data.frame(x=3,y=5), aes(x, y), label="Benchmark", hjust=1, vjust=-.2, colour="#4c4c4c") +
  geom_point(aes(x = SY_mean), color="#FD5B14", fill="#FD5B14", size=4, pch=3) + 
  geom_point(aes(x = PSELI_mean), color="#2B85BA", fill="#2B85BA", size=4, pch=3) +
  geom_point(aes(x = mean_2017), color="#BCA8DC", fill="#BCA8DC", size=4, pch=16) + 
  geom_point(color="#612CB5", fill="#612CB5", size=4, pch=16) + 
  #guides(colour = "colorbar", size = "legend", shape = "legend") + 
  xlim(1, 4) +
  ylab("Program Organization \n & Structure") + 
  scale_y_discrete(labels = function(mean_name) sprintf('%-60s', mean_name)) + 
  theme_bw() + 
  theme(
        axis.title.x =element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_text(debug=TRUE), 
    axis.title.y = element_text(angle = 0, vjust = 0.5, margin=margin(0), size=10),
        panel.grid.minor.x = element_line(colour = "#cccccc",
                                          linetype = "solid"),
        panel.grid.major.x = element_line(colour = "#b2b2b2",
                                          linetype = "solid"),
        panel.grid.major.y = element_line(colour = "#7f7f7f",
                                          linetype = "solid"),
        panel.border = element_blank()
  )

#plot(pq1_plop) 

pq2_plop <-  ggplot(pq2_agged, aes(y=mean_name, x=mean_2018)) + 
  geom_vline(xintercept = 3, size = 0.5, color = "#00C4F3") + #Benchmark static line
  geom_point(aes(x = SY_mean), color="#FD5B14", fill="#FD5B14", size=4, pch=3) + 
  geom_point(aes(x = PSELI_mean), color="#2B85BA", fill="#2B85BA", size=4, pch=3) +
  geom_point(aes(x = mean_2017), color="#BCA8DC", fill="#BCA8DC", size=4, pch=16) +
  geom_point(color="#612CB5", fill="#612CB5", size=4, pch=16) +  
  xlim(1, 4) +
  ylab("Supportive Environment") + 
  scale_y_discrete(labels = function(mean_name) sprintf('%60s', mean_name)) +
  theme_bw() + 
  theme(
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x  = element_blank(),
        axis.title.y = element_text(angle = 0, vjust = 0.5, margin=margin(-5)),
        panel.grid.minor.x = element_line(colour = "#cccccc",
                                          linetype = "solid"),
        panel.grid.major.x = element_line(colour = "#b2b2b2",
                                          linetype = "solid"),
        panel.grid.major.y = element_line(colour = "#7f7f7f",
                                          linetype = "solid"),
        panel.border = element_blank()
  )

#plot(pq2_plop)

pq3_plop <-  ggplot(data = pq3_agged, aes(y=mean_name, x=mean_2018,fill='lightgreen')) + 
  geom_vline(xintercept = 3, size = 0.5, color = "#00C4F3") + #Benchmark static line
  geom_point(aes(x = SY_mean), color="#FD5B14", fill="#FD5B14", size=4, pch=3) + 
  geom_point(aes(x = PSELI_mean), color="#2B85BA", fill="#2B85BA", size=4, pch=3) +
  geom_point(aes(x = mean_2017), color="#BCA8DC", fill="#BCA8DC", size=4, pch=16) +
  geom_point(color="#612CB5", fill="#612CB5", size=4, pch=16) + 
  xlim(1, 4) +
  ylab("Engagement in Activities \n and Learning") + 
  scale_fill_identity(name = 'the fill', guide = 'legend', labels = c('m1')) +
  scale_colour_manual(name = 'the colour', 
                      values =c('black'='black','red'='red'), 
                      labels = c('c2','c1')) + 
  #scale_y_discrete(labels = function(mean_2018) str_wrap(mean_2018, width = 60)) + 
  scale_y_discrete(labels = function(mean_name) sprintf('%-60s', mean_name)) +
  theme_bw() + 
  theme(
        axis.title.x = element_blank(),
        #axis.line.x = element_blank(),
        axis.text.x = element_text(size = 8), 
        axis.text.y = element_text(margin=margin(0), debug=TRUE), 
    axis.title.y = element_text(angle = 0, vjust = 0.5, margin=margin(0), size=10),  
        panel.grid.minor.x = element_line(colour = "#cccccc",
                                          linetype = "solid"),
        panel.grid.major.x = element_line(colour = "#b2b2b2",
                                          linetype = "solid"),
        panel.grid.major.y = element_line(colour = "#7f7f7f",
                                          linetype = "solid"),
        panel.border = element_blank()
  )

图片:

enter image description here

0 个答案:

没有答案