相关疑难解决方法(0)

ggplot2:在每个方面重新排序从最高到最低的条形

df下面,我想在每个方面重新排序从最高到最低的条形

我试过了

df <- df %>% tidyr::gather("var", "value", 2:4)
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity")+facet_wrap(~var, ncol =3)
Run Code Online (Sandbox Code Playgroud)

它给了我 在此输入图像描述

它没有在每个方面从最高到最低排序.

我想出了另一种获得我想要的方法.我不得不一次绘制每个变量,然后使用组合所有绘图grid.arrange()

#I got this function from @eipi10's answer
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937  
#Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot) {
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity") …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-bar

6
推荐指数
2
解决办法
3525
查看次数

如何在ggplot facets中按值排序数据

我有以下数据框:

library(tidyverse)

tdat <- structure(list(term = c("Hepatic Fibrosis / Hepatic Stellate Cell Activation", 
"Cellular Effects of Sildenafil (Viagra)", "Epithelial Adherens Junction Signaling", 
"STAT3 Pathway", "Nitric Oxide Signaling in the Cardiovascular System", 
"LXR/RXR Activation", "NF-?B Signaling", "PTEN Signaling", "Gap Junction Signaling", 
"G-Protein Coupled Receptor Signaling", "Role of Osteoblasts, Osteoclasts and Chondrocytes in Rheumatoid Arthritis", 
"Osteoarthritis Pathway", "VDR/RXR Activation", "Axonal Guidance Signaling", 
"Basal Cell Carcinoma Signaling", "Putrescine Degradation III", 
"Tryptophan Degradation X (Mammalian, via Tryptamine)", "Factors Promoting Cardiogenesis in Vertebrates", 
"Dopamine Degradation", …
Run Code Online (Sandbox Code Playgroud)

r facet ggplot2 facet-grid

6
推荐指数
1
解决办法
445
查看次数

facet_wrap 中因子级别的顺序

我想生成一个facet_wrap,其中构面内因子的顺序基于列因子顺序之一。问题的核心是每个组都有重复的因子水平,当我绘制时,只有一个因子水平在facet_wrap. (见下图)

我尝试对每个组中的因子级别进行排序,并且每个因子级别应在每个方面内正确排序。

这是我的尝试

df_pattern<- data.frame(address = rep(rep(LETTERS[1:3]),3)) 

df_TP <- data.frame(No=rep(seq(1:3)),
                    clas=c("Good","Bad","Ugly"),stringsAsFactors = F)

set.seed(12)
df_ex <- df_pattern%>%
  mutate(No=rep(seq(1:3),each=3))%>%
  left_join(df_TP)%>%
  mutate(clas=sample(clas))%>%
  group_by(No)

#      address    No  clas
#       <fctr> <int> <chr>
#    1       A     1  Good
#    2       B     1  Ugly
#    3       C     1  Ugly
#    4       A     2  Good
#    5       B     2  Ugly
#    6       C     2   Bad
#    7       A     3   Bad
#    8       B     3   Bad
#    9       C     3  Good
Run Code Online (Sandbox Code Playgroud)

现在让我们尝试根据用户定义的类列顺序对地址级别进行排序

set.seed(12)
df_ex …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

3
推荐指数
2
解决办法
8652
查看次数

标签 统计

ggplot2 ×3

r ×3

facet ×1

facet-grid ×1

geom-bar ×1