【问题标题】:Order Y-axis ggplot boxplot based on dataframe column根据数据框列订购 Y 轴 ggplot boxplot
【发布时间】:2018-03-28 22:58:24
【问题描述】:

我用 ggplot 制作了一个箱线图,但我想根据我使用汇总统计创建的不同数据框中的列的顺序来更改 y 轴的顺序。

这是脚本。脚本下方是我想要的输出的描述。

#data
df <- data.frame(City = c("NY", "AMS", "BER", "PAR", "NY", "AMS", "AMS", "PAE"),
             Time_Diff = c(4, 2, 7, 9, 2, 1, 10, 9),
             Outliers = c(0, 0, 0, 0, 0, 1, 1, 0))

#data summary
summary <- df %>%
        group_by(City) %>%
        summarise(Median = median(Time_Diff),
        IQR = IQR(Time_Diff),
        Outliers = sum(Outliers))    %>%
        arrange(desc(Median), desc(IQR), desc(Outliers))

summary <- as.data.frame(summary)


# Create ggplot object
bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots

# Create boxplot figure
bp + 
  geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
  coord_flip(ylim = c(0, 25)) +
  geom_hline(yintercept = 4) +
  ggtitle("Time Difference") +
  ylab("Time Difference") +
  xlab("City") +
  theme_light() +
  theme(panel.grid.minor = element_blank(),
    panel.border = element_blank(), #remove all border lines
    axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
    axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) #add y-axis border line

我希望 y 轴(翻转的 x 轴)的顺序与摘要数据框中 City 列的顺序相同。这意味着:

从上到下:PAE、PAR、BER、NY、AMS

有什么高效优雅的建议吗?

解决方案

谢谢 Prradep,我使用了你的脚本解决方案,它可以工作。我稍微调整了一下,这样我就不必再次输入轴的值了。我重新使用了数据框中的 City 向量。这是我使用的脚本:

#data
df <- data.frame(City = c("NY", "AMS", "BER", "PAR", "NY", "AMS", "AMS", "PAE"),
             Time_Diff = c(4, 2, 7, 9, 2, 1, 10, 9),
             Outliers = c(0, 0, 0, 0, 0, 1, 1, 0))

#data summary
summary <- df %>%
  group_by(City) %>%
  summarise(Median = median(Time_Diff),
        IQR = IQR(Time_Diff),
        Outliers = sum(Outliers))    %>%
  arrange(desc(Median), desc(IQR), desc(Outliers))

summary <- as.data.frame(summary)

# Preproces data for figure
order_city <- summary$City

# Create ggplot object
bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots

# Create boxplot figure
bp + 
  geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
  coord_flip(ylim = c(0, 25)) +
  geom_hline(yintercept = 4) +
  ggtitle("Time Difference") +
  ylab("Time Difference") +
  xlab("City") +
  theme_light() +
  theme(panel.grid.minor = element_blank(),
    panel.border = element_blank(), #remove all border lines
    axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
    axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) + #add y-axis
  scale_x_discrete(limits = rev(order_city)) #this is the function to change the order of the axis

【问题讨论】:

    标签: r ggplot2 axis boxplot


    【解决方案1】:

    添加scale_x_discrete(limits = rev(c('PAE', 'PAR', 'BER', 'NY', 'AMS'))) 就可以了。


    这是你要找的东西吗:

    # Create ggplot object
    bp <-ggplot(data = df, aes(x = reorder(City, Time_Diff, FUN = median), y= Time_Diff)) # Creates boxplots
    
    # Create boxplot figure
    bp + 
      geom_boxplot(outlier.shape = NA) + #exclude outliers to increase visibility of graph
      coord_flip(ylim = c(0, 25)) +
      geom_hline(yintercept = 4) +
      ggtitle("Time Difference") +
      ylab("Time Difference") +
      xlab("City") +
      theme_light() +
      theme(panel.grid.minor = element_blank(),
            panel.border = element_blank(), #remove all border lines
            axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
            axis.line.y = element_line(size = 0.5, linetype = "solid",     colour = "black")) + #add y-axis border line 
      scale_x_discrete(limits = rev(c('PAE', 'PAR', 'BER', 'NY', 'AMS')))
    

    【讨论】:

    • 谢谢!我对您的代码稍作修改以获得最终解决方案。查看我的代码问题
    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2016-12-09
    • 2019-09-13
    • 2014-02-14
    • 2017-07-18
    相关资源
    最近更新 更多