如何在ggplot2中修复混乱的x轴标签?

时间:2017-09-12 23:29:38

标签: r plot ggplot2

我有一个图表,其中x轴标签不均匀。使用axis.text.x = element_text(),我将标签旋转-45度并在vjust = 1处将它们向上移动。但是,x轴上的字本身是混乱的,并且每个单独的标签似乎没有均匀旋转。我怎样才能解决这个问题?

首先,字母似乎是不均匀的。注意艾森豪威尔的“nh”,卡特的“Ca”和“rt”,以及W_Bush的“Bu”。其次,标签上的文字本身并没有均匀的角度,从卡特到W_Bush似乎特别明显。

can be found here

代码:

bp <-
  ggplot(df1, aes(
  x = df1$President,
  y = df1$Count,
  fill = df1$President
)) + geom_bar(stat = "identity", width = 0.5) + theme(axis.text.x = element_text(
  angle = -45,
  hjust = 0,
  vjust = 1
)) + labs(
  title = "Executive Orders Issued by President",
  subtitle = "After World War II",
  x = "President",
  y = "Count"
) + theme(legend.position = "none")

提前致谢!

1 个答案:

答案 0 :(得分:0)

在这种情况下,我会使用coord_flip将名称放在y轴上,避免需要旋转。我也会使用单一颜色的条形图并考虑重新排序。

# Example data:

presidents <- structure(list(President = c("George Washington", "John Adams", 
                                           "Thomas Jefferson", "James Madison", 
                                           "James Monroe", "John Quincy Adams", 
                                           "Andrew Jackson", "Martin van Buren", 
                                           "William Henry Harrison", "John Tyler"), 
                             Number = c(8, 1, 4, 1, 1, 3, 12, 10, 0, 17)), 
                             row.names = c(NA, 10L), 
                             class = "data.frame", 
                             .Names = c("President", "Number"))

presidents %>% 
    ggplot(aes(reorder(President, Number), Number)) + 
      geom_col(fill = "skyblue3") + 
      coord_flip() +
      labs(x = "President")

enter image description here