【问题标题】:Reordering the x axis levels on a bar plot in R / ggplot2 and changing the scale of the y axis在 R / ggplot2 的条形图上重新排序 x 轴级别并更改 y 轴的比例
【发布时间】:2021-01-12 05:27:16
【问题描述】:

我想在 R / ggplot2 的条形图上重新排序 x 轴级别。我还想更改 y 轴的比例。我现在不知道该怎么做。

我从 mt_cars 数据集开始。我创建了 cyl 和 vs 变量的文本版本。

然后我创建了一个条形图,其中 x=cyl_text、y=disp、fill=vs_text。

目前,从左到右依次是“八缸”、“四缸”和“六缸”。我想将它重新排序为任何顺序,特别是以下顺序:(左)“四缸”,“六缸”,然后是“八缸”。这可以做到吗?请指教。

我还想更改 y 轴的比例。目前,刻度从 0 标记为最小刻度,从 400 标记为最大刻度。最大排量的汽车是 472,所以最高的条在刻度最大标记之上。我想将最小刻度标记更改为 -50,将最大刻度标记更改为 600。可以这样做吗?请指教。

提前谢谢。


这是条形图:

这是我用来制作条形图的代码:

# creates bar plot where x=cyl_text, y=disp, fill=vs_text, so that x level order and y scale mix and max can be changed

## dataset of interest
mtcars

### loads ggplot2 package
library(ggplot2)

### colnames of mtcars
colnames(mtcars)

### max value of mtcars$disp
max(mtcars$disp)

#### creates unique text based values of cyl

##### unique values of mtcars$cyl
unique(mtcars$cyl)

##### creates  unique text based values of cyl
mtcars$cyl_text <- ifelse(mtcars$cyl == 4, "Four cylinders",
                          ifelse(mtcars$cyl == 6, "Six cylinders", 
                                 ifelse(mtcars$cyl == 8, "Eight cylinders", NA)
                                 )
                          )

##### gives unique values for mtcars$cyl_text
unique(mtcars$cyl_text)

#### creates test version of mtcars$vs

##### unique values of mtcars$vs
unique(mtcars$vs)

##### creates  unique text based values of vs
mtcars$vs_text <- ifelse(mtcars$vs == 0, "V-shaped engine",
                          ifelse(mtcars$vs == 1, "straight engine", NA)
                          )

##### gives unique values for mtcars$vs_text
unique(mtcars$vs_text)

#### creates base plot
## ---- NOTE: object name will need to be updated when changing variables
## ---- NOTE: dataset used: mtcars
bar_plot__mtcars <- 
  ggplot(mtcars, aes(x=cyl_text, y=disp, fill=vs_text)) + 
  geom_bar(stat="identity", position=position_dodge(), width = .3)
bar_plot__mtcars

#### adds labeles
## ---- NOTE: object name will need to be updated when changing variables
bar_plot__mtcars <- bar_plot__mtcars + ggtitle("Comparing number of cylinders and engine displacement\n while highlighting cars with different engine shapes") +
  xlab("Number of cylinders") + ylab("engine displacement") + theme(plot.title = element_text(hjust = 0.5))
bar_plot__mtcars
bar_plot__mtcars <- 
  bar_plot__mtcars + theme(
    panel.background = element_rect(fill = "white",
                                    colour = "white",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                    colour = "lightblue"), 
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                    colour = "lightblue")
  )
bar_plot__mtcars
bar_plot__mtcars <- 
  bar_plot__mtcars + labs(fill = "Engine shape")
bar_plot__mtcars

【问题讨论】:

标签: r ggplot2 plot bar-chart axis


【解决方案1】:

ggplot 默认会按字母顺序放置文本标签。您可以通过将文本转换为有序因子来强制排序。例如:

mtcars$cyl_text <- factor(mtcars$cyl, c(4, 6, 8), c('Four Cylinders', 'Six Cylinders', 'Eight Cylinders'), ordered = T)

请注意,这也避免了使用嵌套的 ifelse 调用,这会变得非常混乱。

你也可以添加scale_y_continuous(limits = c(-50, 600))来改变y轴的范围。

【讨论】:

    【解决方案2】:

    尝试使用factor() 格式化因子并在限制中添加scale_y_continuous()

    ## dataset of interest
    mtcars
    ### loads ggplot2 package
    library(ggplot2)
    
    ##### creates  unique text based values of cyl
    mtcars$cyl_text <- factor(mtcars$cyl,levels=c(4,6,8),
                              labels = c('Four cylinders','Six cylinders','Eight cylinders'),
                              ordered = T)
    ##### creates  unique text based values of vs
    mtcars$vs_text <- ifelse(mtcars$vs == 0, "V-shaped engine",
                             ifelse(mtcars$vs == 1, "straight engine", NA)
    )
    #### creates base plot
    bp <- ggplot(mtcars, aes(x=cyl_text, y=disp, fill=vs_text)) + 
      geom_bar(stat="identity", position=position_dodge(), width = .3)+
      scale_y_continuous(limits = c(-50,600))+
      ggtitle("Comparing number of cylinders and engine displacement\n
              while highlighting cars with different engine shapes") +
      xlab("Number of cylinders") +
      ylab("engine displacement") +
      theme(plot.title = element_text(hjust = 0.5))+
      theme(
        panel.background = element_rect(fill = "white",
                                        colour = "white",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                        colour = "lightblue"), 
        panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                        colour = "lightblue")
      )+labs(fill = "Engine shape")
    

    输出:

    【讨论】:

      猜你喜欢
      • 2018-11-20
      • 1970-01-01
      • 2021-12-06
      • 2018-01-21
      • 2018-12-15
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多