R语言 如何手动更改条形图之间的间距?

xzv2uavs  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(142)

我用ggplot2和ggh4x包得到了一个图。让我担心的是,图应该是按分组信息绘制的,但现在它不是,因为我用ggh4x包在x aixs上添加了更多的标签。
所以我想知道是否有一种方法来改变酒吧之间的空间根据分组信息。
我的示例数据和代码在这里:

data<-structure(list(Organ = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                         2L, 2L, 2L), .Label = c("Brain\n(GSE123968)", "Brain\n(GSE95401)", 
                                                                 "Brain\n(E-MTAB-6910)", "Lung\nA_(GSE71159)\nC_(E-MTAB-6595)\nL_(GSE139818)\nH_(GSE137501)\nC_(GSE84345)", 
                                                                 "Bone\nT_(E-MTAB-6872)\nP_(E-GEOD-61636)", "Embryo\n(E-GEOD\n-69276)", 
                                                                 "Kidney\nD_(GSE123177)\nA_(GSE135442)", "Aorta\n(E-MTAB\n-7148)", 
                                                                 "Liver\nTumor_\n(E-MTAB\n-4842)\nNASH_\n(GSE119340)", "Retina\n(GSE\n144168)", 
                                                                 "Mus\n(GSE\n14\n15\n62)", "Lym\n(GSE\n12\n93\n15)"), class = "factor"), 
                     Name = structure(1:10, .Label = c("1_Control", "1_Krit1", 
                                                       "2_Control", "2_Pdcd20", "3_Control", "3_Acute", "3_Subacute", 
                                                       "3_Chronic", "4_Control", "4_Acute", "4_Subacute", "4_Chronic", 
                                                       "5_Control", "5_Acute", "5_Subacute", "5_Chronic", "6_Control_acute", 
                                                       "6_Acute", "7_Control_subacute", "7_Subacute", "8_Control_chronic", 
                                                       "8_Chronic", "9_Control_DG", "9_DG_VEGF", "10_Control_CA1", 
                                                       "10_CA1_VEGF", "11_Control_Ectrl", "11_Control_EfCtrl", "11_E3KO", 
                                                       "12_Control", "12_Cpt1_ko", "13_Control", "13_Tsc2_ko", "14_Control", 
                                                       "14_Zmpste24_ko", "15_Control", "15_AM_ko", "16_Control", 
                                                       "16_Chemotherapy", "16_Radiation", "16_Young", "16_Aged", 
                                                       "17_EC in bone", "18_Control", "18_N1_plus_vg", "18_N1_plus_12", 
                                                       "18_N1_plus_-", "18_N1_12_-", "19_Control", "19_STZ", "20_Control", 
                                                       "20_AS_dimtdT", "21_Control", "21_AS_brighttdT", "22_Control", 
                                                       "22_Differentiated", "23_Control", "23_Liver  tumor", "24_Control", 
                                                       "24_Nash", "25_Control", "25_EAU", "26_Control", "26_ iDUX4", 
                                                       "27_Control", "27_RANKL_ko"), class = "factor"), Disease = structure(c(1L, 
                                                                                                                              1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("CCM", "Seizure", 
                                                                                                                                                                              "EAE", "Stroke", "TBI", "Infla", "AOD", "CVD", "LAM", "HGPS", 
                                                                                                                                                                              "Tumor", "Pancy", "Carcino", "DK", "AS", "Home", "NASH", 
                                                                                                                                                                              "EAU", "FSHD"), class = "factor"), fill = c("#FF0000", "#FF0000", 
                                                                                                                                                                                                                          "#00FF00", "#00FF00", "#7570B3", "#7570B3", "#7570B3", "#7570B3", 
                                                                                                                                                                                                                          "#A6D854", "#A6D854"), mean = c(806.898912005764, 837.595854895274, 
                                                                                                                                                                                                                                                          898.32650562378, 2208.81852805305, 212.969188902024, 195.692360171933, 
                                                                                                                                                                                                                                                          181.622170748826, 454.592532593114, 477.034742547228, 383.52324684751
                                                                                                                                                                                                                          ), sd = c(108.62191806741, 86.1336851228738, 108.18221445462, 
                                                                                                                                                                                                                                    551.492848636393, 35.6423740115715, 28.0730270395565, 42.7691784348475, 
                                                                                                                                                                                                                                    116.005320530673, 43.8700365938649, 63.2689449323908)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                          10L), class = "data.frame")
data

ggplot(data = data, aes(weave_factors(Name,Disease,Organ), mean,fill=fill))+
  geom_bar(position="dodge", stat="identity",width = 0.9,color="black") +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.9), width = .2) +
  theme_classic2() +
  theme(legend.position = "none") +
  theme(axis.text.x=element_text(vjust=1,size=10))+
  labs(title = "Igfbp7", x = NULL, y = "FPKM_value") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_discrete(guide = "axis_nested")+
  theme(plot.margin = unit(c(5, 20, 30, 7), "mm"))+
  #  rotate_x_text(angle = 45)+
  guides(x = ggh4x::guide_axis_nested(angle = -45))

字符串
上面的代码运行良好。
我想用“疾病”来改变空间。
你能给予我一些建议吗。

camsedfj

camsedfj1#

这里是一个尝试使用您的数据

library(dplyr)
library(ggplot2)
library(ggh4x)

# Create a width variable based on Disease group_index multiply by .15
data$width <- data %>% group_by(Disease) %>% group_indices() * .15

# Draw the plot with width as aes mapped to the created variable
ggplot(data = data, aes(weave_factors(Name,Disease,Organ), mean, fill=fill, width = width))+
  geom_bar(position="dodge", stat="identity", color="black") +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.9), width = .2) +
  theme(legend.position = "none") +
  theme(axis.text.x=element_text(vjust=1,size=10))+
  labs(title = "Igfbp7", x = NULL, y = "FPKM_value") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_discrete(guide = "axis_nested")+
  theme(plot.margin = unit(c(5, 20, 30, 7), "mm"))+
  #  rotate_x_text(angle = 45)+
  guides(x = ggh4x::guide_axis_nested(angle = -45))

字符串
x1c 0d1x的数据
创建于2021-04-01由reprex package(v1.0.0)

相关问题