相关疑难解决方法(0)

ggplot2 - 叠加和闪避的条形图

我正在尝试创建一个条形图,使用ggplot2我在一个变量堆叠并由另一个变量躲避的地方.

这是一个示例数据集:

df=data.frame(
  year=rep(c("2010","2011"),each=4),
  treatment=rep(c("Impact","Control")),
  type=rep(c("Phylum1","Phylum2"),each=2),
  total=sample(1:100,8))
Run Code Online (Sandbox Code Playgroud)

我想创建一个条形图,其中x=treatment,y=total堆叠变量是type,并且躲闪变量是year.当然我可以做其中一个:

ggplot(df,aes(y=total,x=treatment,fill=type))+geom_bar(position="dodge",stat="identity")

ggplot(df,aes(y=total,x=treatment,fill=year))+geom_bar(position="dodge",stat="identity")
Run Code Online (Sandbox Code Playgroud)

但不是两个!感谢任何能提供建议的人.

r ggplot2

30
推荐指数
5
解决办法
2万
查看次数

删除条形ggplot2之间的空格

这是我的代码

    ggplot(df, aes(x=timepoint, y=mean, fill=group)) +
            geom_bar(position=position_dodge(.3), colour="black", stat="identity", width=0.3, , binwidth=0) +
            geom_errorbar(position=position_dodge(.3), width=.25, aes(ymin=mean, ymax=mean+sem)) +
            scale_fill_manual(values=c("#FFFFFF", "#000000"), guide=FALSE) +
            theme_bw() +
            ylab(ylab) +
            xlab("") +
            # xlim("Baseline", "12w") +
            scale_x_discrete(expand = c(0,0), limits=c("Baseline","12w")) + 
            scale_y_continuous(expand = c(0,0) ) + 
            theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
                  panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Run Code Online (Sandbox Code Playgroud)

这是我的输出,我不想要,它在"基线"和"12w"之间的空间太大:

在此输入图像描述

如何去除栏之间的空间?

谢谢

df是这样的:

df <- structure(list(group = c("a1.d.ffa.mean Dysglyc", "a1.c.ffa.mean Control", 
"b1.d.ffa.mean Dysglyc", "b1.c.ffa.mean Control"), timepoint = c("Baseline", 
"Baseline", "12w", "12w"), mean = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

11
推荐指数
1
解决办法
2万
查看次数

ggplot2 geom_bar position ="dodge"不闪避

我有以下数据框p3:

         test     result
    1      1    26.87778
    2      1    24.52598
    3      1    24.02202
    4      1    20.32632
    5      1    22.00618
    6      2    19.84013
    7      2    19.68983
    8      2    19.84013
    9      2    19.23892
    10     2    19.23892
    11     3    34.36430
    12     3    33.28196
    13     3    33.82313
    14     3    33.82313
    15     3    32.47020
    16     4    25.55169
    17     4    26.90442
    18     4    25.40138
    19     4    24.19895
    20     4    25.85230
    21     4    25.70199
    22     4    24.95047
    23     5    18.64646
    24     5    18.64646
    25     5    17.80653
    26     5 …
Run Code Online (Sandbox Code Playgroud)

r bar-chart ggplot2

8
推荐指数
2
解决办法
1万
查看次数

绘制堆积条形图?

我有以下数据:

structure(list(Time = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L, 7L, …
Run Code Online (Sandbox Code Playgroud)

charts r bar-chart ggplot2

3
推荐指数
1
解决办法
3419
查看次数

标签 统计

ggplot2 ×4

r ×4

bar-chart ×2

charts ×1