Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中组合堆栈和组条形图_R_Ggplot2_Stack_Grouping_Bar Chart - Fatal编程技术网

在R中组合堆栈和组条形图

在R中组合堆栈和组条形图,r,ggplot2,stack,grouping,bar-chart,R,Ggplot2,Stack,Grouping,Bar Chart,我的数据框看起来像: year A B C 1983 1 2 10 1983 2 3 7 1984 1 3 7 1984 2 4 8 1985 1 6 6 1985 2 5 10 我想生成一个由a分组的条形图,并将B的值显示为C的子集,如下所示: 你知道怎么做吗?建议使用dplyr、tidyr和ggplot2facet\uuu: df <- read.table(text='year A B C 1983 1 2 10 1983 2 3 7 1984 1 3 7

我的数据框看起来像:

year A B C
1983 1 2 10
1983 2 3 7
1984 1 3 7
1984 2 4 8
1985 1 6 6
1985 2 5 10
我想生成一个由a分组的条形图,并将B的值显示为C的子集,如下所示:


你知道怎么做吗?

建议使用
dplyr
tidyr
ggplot2
facet\uuu

df <- read.table(text='year A B C
    1983 1 2 10
    1983 2 3 7
    1984 1 3 7
    1984 2 4 8
    1985 1 6 6
    1985 2 5 10', header=TRUE, stringsAsFactors=FALSE)

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
  pivot_longer(cols = c(B, C)) %>% 
  ggplot(aes(factor(A), value, fill=factor(name, levels= c("C", "B")))) +
  geom_bar(stat="identity", position="stack") +
  facet_grid(~year) +
  labs(x="A", fill="Variable")
df%
枢轴长度(cols=c(B,c))%>%
ggplot(aes(因子(A),值,填充=因子(名称,级别=c(“c”,“B”)))+
几何图形栏(stat=“identity”,position=“stack”)+
平面网格(~年)+
实验室(x=“A”,fill=“变量”)

工作正常!!如何调整C的不同值的颜色,同时在整个图形中保持B的相同颜色?