簡體   English   中英

在 R / ggplot2 中的條形圖上重新排序 x 軸級別並更改 y 軸的比例

[英]Reordering the x axis levels on a bar plot in R / ggplot2 and changing the scale of the y axis

我想在 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

默認情況下,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 軸的范圍。

嘗試使用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")

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM