简体   繁体   English

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

[英]How can I fix my jumbled x-axis labels in ggplot2?

I have a plot in which the x-axis labels are not uniform. 我有一个x轴标签不一致的图。 Using axis.text.x = element_text(), I rotated the labels -45 degrees and moved them up at vjust = 1. However, the words on the x-axis themselves are jumbled, and each individual label does not seem uniformly rotated. 使用axis.text.x = element_text(),我将标签旋转了-45度,并在vjust = 1处将它们向上移动。但是,x轴上的单词本身杂乱无章,每个标签似乎都没有统一旋转。 How can I fix this? 我怎样才能解决这个问题?

First, the letters seem to be unevenly spaced. 首先,字母似乎间隔不均匀。 Note "nh" in Eisenhower, "Ca" and "rt" in Carter, and "Bu" in W_Bush. 在艾森豪威尔(Eisenhower)中注意“ nh”,在卡​​特(Carter)中注意“ Ca”和“ rt”,在W_Bush中注意“ Bu”。 Second, the words on the label themselves are not evenly angled, and it seems especially noticeable from Carter to W_Bush. 其次,标签上的单词本身并不是均匀倾斜的,从卡特(Carter)到W_Bush,这似乎特别值得注意。

可以在这里找到

Code: 码:

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")

Thanks in advance! 提前致谢!

In this case I would use coord_flip to place the names on the y-axis, avoiding the need for rotation. 在这种情况下,我将使用coord_flip将名称放置在y轴上,从而避免了旋转。 I'd also use a single color for the bars and consider reordering. 我也将对条使用单一颜色并考虑重新排序。

# 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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM