簡體   English   中英

ggplot:在x軸上具有自己的因子順序的方面

[英]ggplot: facets with their own order of factors on x axis

有很多類似的問題,但沒有找到我想要的東西。 我有以下數據集:

dat<-data.frame(Pal=rep(c("A","B","C","D"),each=5),
            Rol=c("aa","aa","aa","aa","bb","aa","aa","aa","aa","aa","cc","cc","cc","cc","cc","aa","aa","aa","aa","aa"),
            Cel=rep(c("home","tree","hat","ball","pen","rope"),times=c(5,3,2,5,2,3)),
            Value=c(7701.1,59897.3,59897.3,59897.3,744438.1,1226.4,1454.6,1454.6,1454.6,1454.6,56600,92400,5010000,7010000,15740000,28.5,34.2,39.9,48.5,57),
            Col=c("black","red","black","black","red","red","red","black","black","black","red","red","red","red","red","red","black","black","black","black"),
            Effect=c("length","length","length","length","height","weight","length","length","age of youngest individual found miles from the closest coastline","age of youngest individual found miles from the closest coastline","pressure","speed","rate","rate","length","length","rate","rate","O2","fecundity")
            )

我正在嘗試構建一個稍微復雜的圖表,如下所示:

highlights<-data.frame(Pal=c("A","B","C","D"))
highlights$Pal<-factor(highlights$Pal,levels=c("A","B","C","D"))

ggplot() + 
  geom_rect(data=highlights,aes(xmin=-Inf, xmax=Inf, ymin=1, ymax=1000000000), fill=c("yellow","blue","red","green"), alpha=0.05) +
  geom_point(data = dat, aes(x=Effect, y=Value, shape=Rol, col=Col)) +
  scale_color_manual(breaks=unique(dat$Col), values=as.character(unique(dat$Col))) +
  labs(x="",y="Activity") + facet_grid(.~Pal, scales = "free_x")+
  scale_y_log10(limits=c(1,1000000000),breaks = c(1,10,100,1000,10000,100000,1000000,10000000,100000000,100000000,1000000000)) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        panel.background = element_rect(fill = "white", colour = "white"),
        strip.background = element_rect(fill = "white", colour = "black"),
        legend.key = element_rect(fill = "white")) 

在此處輸入圖像描述

我希望數據點在每個方面都以遞增的順序排列。 由於數據已經以這種方式排序,我嘗試使用“facetscales”庫中的“facet_grid_sc”函數根據數據中唯一的“效果”列創建單獨的 facet x 軸比例,如下所示:

my_scales <- list(
  "A" = scale_x_discrete(limits = as.character(unique(dat[which(dat$Pal=="A"),"Effect"]))),
  "B" = scale_x_discrete(limits = as.character(unique(dat[which(dat$Pal=="B"),"Effect"]))),
  "C" = scale_x_discrete(limits = as.character(unique(dat[which(dat$Pal=="C"),"Effect"]))),
  "D" = scale_x_discrete(limits = as.character(unique(dat[which(dat$Pal=="D"),"Effect"])))
)


ggplot() + 
  geom_rect(data=highlights,aes(xmin=-Inf, xmax=Inf, ymin=1, ymax=1000000000), fill=c("yellow","blue","red","green"), alpha=0.05) +
  geom_point(data = dat, aes(x=Effect, y=Value, shape=Rol, col=Col)) +
  scale_color_manual(breaks=unique(dat$Col), values=as.character(unique(dat$Col))) +
  labs(x="",y="Activity") + facet_grid_sc(cols = vars(Pal), scales=list(x = my_scales)) +
  scale_y_log10(limits=c(1,1000000000),breaks = c(1,10,100,1000,10000,100000,1000000,10000000,100000000,100000000,1000000000)) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        panel.background = element_rect(fill = "white", colour = "white"),
        strip.background = element_rect(fill = "white", colour = "black"),
        legend.key = element_rect(fill = "white"))

在此處輸入圖像描述

雖然這種方式解決了問題,因為我的數據點按遞增順序排列,但它完全搞砸了情節。 你們中有人對此有解決方案嗎? 高度贊賞

謝謝

您可以使用此GitHub中的reorder_withinscale_x_reordered函數,如下所示:

dat<-data.frame(Pal=rep(c("A","B","C","D"),each=5),
                Rol=c("aa","aa","aa","aa","bb","aa","aa","aa","aa","aa","cc","cc","cc","cc","cc","aa","aa","aa","aa","aa"),
                Cel=rep(c("home","tree","hat","ball","pen","rope"),times=c(5,3,2,5,2,3)),
                Value=c(7701.1,59897.3,59897.3,59897.3,744438.1,1226.4,1454.6,1454.6,1454.6,1454.6,56600,92400,5010000,7010000,15740000,28.5,34.2,39.9,48.5,57),
                Col=c("black","red","black","black","red","red","red","black","black","black","red","red","red","red","red","red","black","black","black","black"),
                Effect=c("length","length","length","length","height","weight","length","length","age of youngest individual found miles from the closest coastline","age of youngest individual found miles from the closest coastline","pressure","speed","rate","rate","length","length","rate","rate","O2","fecundity")
)

library(ggplot2)
library(forcats)

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

highlights<-data.frame(Pal=c("A","B","C","D"))
highlights$Pal<-factor(highlights$Pal,levels=c("A","B","C","D"))

ggplot() + 
  geom_rect(data=highlights,aes(xmin=-Inf, xmax=Inf, ymin=1, ymax=1000000000), fill=c("yellow","blue","red","green"), alpha=0.05) +
  geom_point(data = dat, aes(x=reorder_within(Effect, Value, Pal), y=Value, shape=Rol, col=Col)) +
  scale_color_manual(breaks=unique(dat$Col), values=as.character(unique(dat$Col))) +
  labs(x="",y="Activity") + facet_grid(.~Pal, scales = "free_x")+
  scale_y_log10(limits=c(1,1000000000),breaks = c(1,10,100,1000,10000,100000,1000000,10000000,100000000,100000000,1000000000)) +
  scale_x_reordered() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        panel.background = element_rect(fill = "white", colour = "white"),
        strip.background = element_rect(fill = "white", colour = "black"),
        legend.key = element_rect(fill = "white")) 

reprex 包於 2022-07-20 創建 (v2.0.1)

暫無
暫無

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

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