空のデータスペースを削除し、グループ化された棒グラフで均一な棒幅を維持します。ggplot2でファセットグリッドを使用する

アヴニ

グループ化された棒グラフをプロットするために使用しているコードは次のとおりです。均一な棒幅を維持できますが、使用している場合は欠落データの空のスペースを削除できません。

geom_bar(stat="identity",position=position_dodge(preserve = "single"),na.rm = TRUE,width = 0.9). 

また、上記のコマンドを使用していない場合、空白を削除することはできますが、バーの幅を保持することはできません。

library(ggplot2)
library(reshape2)
library(readxl)
df <- read_excel("Documents/Analysis.xlsx")

ggplot(data = df, aes(x = Cell_type, y = Percentage_of_cell, fill = Sample_id, na.rm = TRUE),
       position = position_dodge(preserve = "single", padding = 0)) + 
  geom_bar(stat = "identity", position = position_dodge(preserve = "single"), na.rm = TRUE, width = 0.9)+
  theme_bw() +
  scale_fill_manual(values=c("darkgoldenrod3","darkolivegreen"))+
  theme(axis.text.x = element_blank(), 
        axis.text.y = element_text(color="black", angle = 90, hjust = 1, size = 12, face = "bold"), 
        axis.title.y = element_text( size = 12, angle = 90, hjust = .5, vjust = .5, face = "bold"),
        legend.text = element_text(size = 12, face = "bold"),
        axis.ticks.x = element_blank())+
  labs(fill="") + 
  xlab("") +
  ylab("% of Cell_type\n") + 
  facet_grid(~Cell_type), 
             scales = "free_x", space = "free_x")+
  theme(plot.margin = unit(c(1,1,1.5,1.2),"cm"))+ 
  theme(strip.text = element_text(colour = 'black', face="bold", size=11))+
  theme(legend.position = "bottom")

これがdfです:

Sample_id Cell_type Percentage_of_cell   
Sample1 Cell1   4.701222662  
Sample2 Cell1   0.829875519  
Sample2 Cell2   5.526216522  
Sample1 Cell3   1.980368521  
Sample2 Cell3   20.50169747  
Sample1 Cell4   22.75701739  
Sample2 Cell4   21.3504338  
Sample1 Cell5   15.00774927  
Sample2 Cell5   9.430403621  
Sample1 Cell6   7.465128293  
Sample1 Cell7   0.602720854  
Sample2 Cell7   1.772915881  
Sample1 Cell8   44.07611503  
Sample2 Cell8   40.58845719  
Sample1 Unassigned  3.409677975  

私はRを初めて使用します。どこが間違っているのか、またはこの問題に対する他の解決策があるかどうかを指摘するのを手伝ってください。

江戸

これはあなたが探しているものですか?

ご了承ください:

  • xaes今ですSampl_id
  • position_dodge 削除されました
  • geom_colgeom_bar必要な場合よりも言葉が少ないstat=identity
  • themesとlabsはすべて一緒になりました
  • scalesそして、spacefacet_grid、今の仕事が予想通り
ggplot(data = df, aes(x = Sample_id, y = Percentage_of_cell, fill = Sample_id)) +
 geom_col(width = 0.9) +
 facet_grid(cols = vars(Cell_type), 
            labeller = label_wrap_gen(width = 16,multi_line = TRUE), 
            scales="free", space = "free") +
 labs(x = "", y = "% of Cell_type\n", fill = "") +
 scale_fill_manual(values = c("darkgoldenrod3", "darkolivegreen")) +
 theme_bw() +
 theme(axis.text.x  = element_blank(), 
       axis.text.y  = element_text(color = "black", angle = 90, hjust = 1, size = 12, face = "bold"), 
       axis.title.y = element_text(size = 12, angle = 90, hjust = 0.5, vjust = 0.5, face = "bold"),
       legend.text  = element_text(size = 12, face = "bold"),
       axis.ticks.x = element_blank(),
       plot.margin  = unit(c(1,1,1.5,1.2),"cm"),
       strip.text   = element_text(colour = 'black', face = "bold", size = 11),
       legend.position = "bottom")

ここに画像の説明を入力してください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ