簡體   English   中英

ggplot2:更改繪圖中構面分組變量的標簽

[英]ggplot2: Change label for facet grouping variable in plot

這篇文章顯示了如何使用多個散點圖制作構面,但是我試圖更改名稱grp ,該名稱位於組名及其相應顏色的頂部,並且我嘗試使用ggplot2選項更改它而不更改數據框中的名稱。 我要這樣做的原因是我希望顯示的新標簽在其中留有空格。 例如,我希望將grp更改為The Groups

謝謝你的幫助。

由於問題是針對此帖子的 ,因此我將解決方案添加到此處給出的代碼中:

  # get Petal.Length for each species separately    
  df1 <- subset(iris, Species == "virginica", select=c(Petal.Length, Species))
  df2 <- subset(iris, Species == "versicolor", select=c(Petal.Length, Species))
  df3 <- subset(iris, Species == "setosa", select=c(Petal.Length, Species))

  # construct species 1 vs 2, 2  vs 3 and 3 vs 1 data
  df <- data.frame(x=c(df1$Petal.Length, df2$Petal.Length, df3$Petal.Length), 
                   y = c(df2$Petal.Length, df3$Petal.Length, df1$Petal.Length), 
                   grp = rep(c("virginica.versicolor", "versicolor.setosa", "setosa.virginica"), each=50))
  df$grp <- factor(df$grp)

  # plot
  require(ggplot2)
  ggplot(data = df, aes(x = x, y = y)) + geom_point(aes(colour=grp)) + facet_wrap( ~ grp) +
     labs(colour="The Groups")

導致下圖:

在此處輸入圖片說明

我添加的用於設置圖例標題的部分是+labs(colour="The Groups") labs也可用於為其他美學設置標題:

+ labs(x="x-axis title", y="y-axis title", fill="fill legend title", shape="shape legend title", colour="colour legend title")

也許其他我忘了的東西

最后一句話:您要求更改“構面分組變量標簽”的名稱。 這實際上不是您要執行的操作,也不是我的解決方案要執行的操作。 它將更改用於着色的變量的標簽( aes(colour=grp) )。 碰巧這個相同的變量也用於facet_wrap( ~ grp)面( facet_wrap( ~ grp) )。

暫無
暫無

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

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