如何防止R在ggplot中按字母顺序排列数据,并指定数据的绘制顺序(提供数据+代码+图形)?

问题描述 投票:1回答:1

我试图解决我的GGBalloonPlot图中关于R如何处理轴标签的问题。

默认情况下,R会使用按字母顺序逆向排列的标签来绘制数据,但是为了揭示数据的模式,数据需要按照特定的顺序来绘制。我一直能够做到欺骗软件的唯一方法是通过手动添加一个前缀到我的.csv表中的每个标签,这样R将在我的输出中正确地排列它们。这是很耗时的,因为我需要在添加前缀之前先手动对数据进行排序,然后再进行绘制。

我想输入一个字符向量(或类似的东西),它基本上可以指定我想让数据绘制的顺序,这将揭示模式,而不需要在标签名称中添加前缀。

我已经用 "scale_y_discrete "做了一些尝试,但没有成功。我也想为X轴做同样的事情,因为我不得不使用同样的 "技巧 "以适当的非字母顺序显示列,这就抵消了标签的位置。有什么办法可以让GGplot在不 "欺骗 "软件的情况下,在图形中显示我的值,因为这很耗时?

数据+代码

#Assign data to "Stack_Overflow_DummyData"

Stack_Overflow_DummyData <- structure(list(Species = structure(c(8L, 3L, 1L, 5L, 6L, 2L, 
                                     7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 
                                     7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L), .Label = c("Ani", "Cal", 
                                                                                         "Can", "Cau", "Fis", "Ort", "Sem", "Zan"), class = "factor"), 
               Species_prefix = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 2L, 
                                            1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L, 
                                            2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("ac.Cau", 
                                                                                                "ad.Sem", "af.Cal", "ag.Ort", "as.Fis", "at.Ani", "be.Can", 
                                                                                                "bf.Zan"), class = "factor"), Dist = structure(c(2L, 3L, 
                                                                                                                                                 5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L, 
                                                                                                                                                 3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L
                                                                                                ), .Label = c("End", "Ind", "Pan", "Per", "Wid"), class = "factor"), 
               Region = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
                                    4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Cen", "Col", 
                                                                                "Far", "Nor"), class = "factor"), Region_prefix = structure(c(1L, 
                                                                                                                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                              3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                              4L), .Label = c("a.Far", "b.Nor", "c.Cen", "d.Col"), class = "factor"), 
               Frequency = c(75, 50, 25, 50, 0, 0, 0, 0, 11.1, 22.2, 55.6, 
                             55.6, 11.1, 0, 5.6, 0, 0, 2.7, 36.9, 27.9, 65.8, 54.1, 37.8, 
                             28.8, 0, 0, 0, 3.1, 34.4, 21.9, 78.1, 81.3)), class = "data.frame", row.names = c(NA, 
                                                                                                               -32L))



# Plot Data With Prefix Trick

library(ggplot2)
library(ggpubr)

# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix", 
              size = "Frequency", size.range = c(1, 9), fill = "Dist") +
  theme_set(theme_gray() + 
  theme(legend.key=element_blank())) + 
  # Sets Grey Theme and removes grey background from legend panel
  theme(axis.title = element_blank()) +
  # Removes X axis title (Region)
  geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) 
# Add Frequency Values Next to the circles

# Plot Data Without Prefix Trick

library(ggplot2)
library(ggpubr)

# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species", 
              size = "Frequency", size.range = c(1, 9), fill = "Dist") +
  theme_set(theme_gray() + 
  theme(legend.key=element_blank())) + 
  # Sets Grey Theme and removes grey background from legend panel
  theme(axis.title = element_blank()) +
  # Removes X axis title (Region)
  geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) 
# Add Frequency Values Next to the circles

以下是图表

好图。

使用标签前缀技巧与数据中的可见模式。

enter image description here

错误的图形(R默认)。

如果没有前缀技巧,当GGplot自动排序的数据标签和图形没有意义。

enter image description here

综上所述,我想好图输出,而不必事先在我的标签中添加前缀。

非常感谢大家的帮助。

r ggplot2 legend axis aesthetics
1个回答
0
投票

对于轴标签,我会定义一个先前的函数来覆盖分界线。

shlab <- function(lbl_brk){
  sub("^[a-z]+\\.","",lbl_brk) # removes the starts of strings as a. or ab.
}

然后,要改变标签,你只需要使用... ... scale_x,y_discretelabels = shlab (如果你看了帮助的 scale_x_discrete 你会看到,其中一个选项是 labelsA function that takes the breaks as input and returns labels as output).

对于颜色的改变就可以了(values)在 scale_fill_manual 而对于尺寸,使用 guides 所以。

library(ggplot2)
library(ggpubr)
shlab <- function(lbl_brk){
  sub("^[a-z]+\\.","",lbl_brk)
}
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix", size = "Frequency", size.range = c(1, 9), fill = "Dist") +
  scale_x_discrete(labels = shlab) +
  scale_y_discrete(labels = shlab) +
  scale_fill_manual(values = c("green", "blue", "red", "black", "white")) +
  guides(fill = guide_legend(override.aes = list(size=8))) +
  theme_set(theme_gray() + theme(legend.key=element_blank())) +     # Sets Grey Theme and removes grey background from legend panel
  theme(axis.title = element_blank()) +                             # Removes X axis title (Region)
  geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) # Add Frequency Values Next to the circles

enter image description here

UPDATE:

有了新的数据集和矢量标签。

library(ggplot2)
library(ggpubr)

# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species", 
              size = "Frequency", size.range = c(1, 9), fill = "Dist") +
  scale_y_discrete(limits = c("Cau", "Sem", "Cal", "Ort", "Fis", "Ani", "Can", "Zan")) +
  scale_x_discrete(limits = c("Far", "Nor", "Cen", "Col")) +
  theme_set(theme_gray() + 
              theme(legend.key=element_blank())) + 
  # Sets Grey Theme and removes grey background from legend panel
  theme(axis.title = element_blank()) +
  # Removes X axis title (Region)
  geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.