如何使用 ggplot2 将金字塔图的顺序更改为数据集顺序?

How to change order for pyramid plots with ggplot2 to dataset order?

我有一个数据集,其中包含当前和未来树种的气候适宜性值 (0-1)。 我想用 ggplot2 包在金字塔图中可视化数据,而现在应该显示在图的左侧,未来应该显示在右侧,树种按照我的原始数据集中给出的顺序显示。

b2010<-read.csv("csi_before2010_abund_order.csv",header=T,sep = ";")

str(b2010)
'data.frame':   20 obs. of  7 variables:
 $ species: Factor w/ 10 levels "Acer platanoides",..: 9 9 7 7 8 8 6 6 5 5 ...
 $ time   : Factor w/ 2 levels "future","present": 2 1 2 1 2 1 2 1 2 1 ...
 $ grid1  : num  0.6001 0.5945 0.6366 0.0424 0.6941 ...
 $ grid2  : num  0.6399 0.5129 0.6981 0.0399 0.711 ...
 $ grid3  : num  0.6698 0.5212 0.6863 0.0446 0.6795 ...
 $ mean   : num  0.6366 0.5429 0.6737 0.0423 0.6949 ...
 $ group  : Factor w/ 1 level "before 2010": 1 1 1 1 1 1 1 1 1 1 ...

b2010$mean = ifelse(b2010$time == "future", b2010$mean * -1,b2010$mean)
head(b2010)
            species    time      grid1      grid2      grid3        mean       group
1    Tilia europaea present 0.60009009 0.63990200 0.66975713  0.63658307 before 2010
2    Tilia europaea  future 0.59452874 0.51294094 0.52115256 -0.54287408 before 2010
3 Sorbus intermedia present 0.63659602 0.69813931 0.68629903  0.67367812 before 2010
4 Sorbus intermedia  future 0.04242327 0.03990654 0.04460707 -0.04231229 before 2010
5     Tilia cordata present 0.69414478 0.71097034 0.67950863  0.69487458 before 2010
6     Tilia cordata  future 0.55790818 0.53918493 0.51979470 -0.53896260 before 2010

ggplot(b2010, aes(x = factor(species), y = mean, fill = time)) +
  geom_bar(stat = "identity") + 
  facet_share(~time, dir = "h", scales = "free", reverse_num = T) +
  coord_flip()

现在,未来和现在的顺序是错误的,物种也是按字母顺序排列的,尽管它们显然是 "factors",因此应该根据我的数据集进行排序。非常感谢您的帮助。 谢谢你和亲切的问候

您误解了因子的作用方式。条形图按照 levels(b2010$species) 打印的顺序绘制。要更改此顺序,您必须手动重新排序,即

b2010$species <- factor(b2010$species, 
                        levels = c("Sorbus intermedia", "Tilia chordata"...))

这些水平自然也可以是某些统计数据的函数,即均值。为此,您需要按照

myorder <- b2010[order(b2010$mean) & b2010$time == "present", "species"]
b2010$species <- factor(b2010$species, levels = myorder)