Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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_Bar Chart - Fatal编程技术网

R 如何使用两个都决定填充的因素创建堆叠条形图?

R 如何使用两个都决定填充的因素创建堆叠条形图?,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,这是我正在处理的数据 structure(list(NewRegion = c("Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic"

这是我正在处理的数据

structure(list(NewRegion = c("Northeastern Atlantic", "Northeastern Atlantic", 
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", 
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", 
"Northeastern Atlantic", "Northeastern Atlantic", "Northeastern Atlantic", 
"Northeastern Atlantic"), New_categories = c("Behaviour", "Behaviour", 
"Environmental conditions", "Environmental conditions", "Habitat use", 
"Habitat use", "Interaction", "Invasive species", "Management", 
"Management", "Movement", "Movement"), New_Water = c("Fresh", 
"Marine", "Fresh", "Marine", "Fresh", "Marine", "Fresh", "Fresh", 
"Fresh", "Marine", "Fresh", "Marine"), count = c(12L, 17L, 10L, 
7L, 5L, 11L, 1L, 12L, 5L, 11L, 18L, 24L)), row.names = c(NA, 
12L), class = "data.frame")
这是我当前的绘图代码

ggplot(EAtl, aes(x = New_categories, y = count, fill = New_categories, color = New_Water)) +
  geom_bar(position = 'stack', stat = 'identity', color = 'black')+ scale_fill_manual(values = colforplots)+
  scale_y_continuous(expand = c(0.01,0.01))+
  theme(panel.background = element_rect(fill = 'transparent', colour = NA),
        plot.background = element_rect(fill = 'transparent', colour = NA),
        panel.grid.minor = element_line(color = 'black'),
        panel.grid.major = element_line(color = 'black'),
        legend.position = 'none', axis.title.x = element_blank(), 
        axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y = element_text(size = 19, face = 'bold'), axis.title.y = element_blank())
此条形图显示了东北大西洋地区各类别的计数。我添加了一个颜色参数来表示新的水类别。但是,这不会改变任何颜色,只会添加一条线,将每个条分成两条。我想保留颜色方案,但每个条的一部分(比如淡水)都有纹理(即斜线)


我发现了一些关于类似主题的老讨论,但没有具体的答案/更新。

这里有几点需要注意,应该可以帮助您:

  • ggplot()
    中设置
    color=
    美学,然后通过设置
    color=“black”
    geom\u bar()中覆盖它。如果删除
    color='black'
    ,您现在将拥有基于“新水”创建的彩色框和图例。这仍然对你没有太大帮助,因为很难区分,但这是问题的一部分

  • ggplot
    中不直接支持填充图案。调用了
    ggpattern
    ,但我无法在我的R版本上安装。看来
    geom\u col\u patterned()
    可能会对您有所帮助

  • 如果不使用
    ggpattern
    的话,一个简单的解决方案是使用
    alpha=
    美学来根据“新水”改变整体颜色强度。这在这里特别有用,因为您只有两个标签:淡水和海水。我将在下面向您展示它的一个实现

使用alpha美学模拟两种“模式”的解决方案 要设置
alpha=
美观,只需在代码的
ggplot()
行中将
color=
更改为
alpha=
。alpha的默认变化看起来不大;但是,为了让这一切顺利进行,我不得不做一些修改:


  • 删除
    主题(legend.position=“无”
    参数
    color
    表示条边的颜色,而不是填充条内的颜色,如果在
    geom\u bar
    中删除参数
    color=''black'
    ,您将看到差异。在我看来,您已经有了指示类别的x轴,也许您不需要填充不同的类别为了强调这一点,我的建议是
    fill=New\u water
    。以下是我建议的前两行代码:
    ggplot(EAtl,aes(x=New\u categories,y=count,fill=New\u water))+geom\u bar(position='stack',stat'identity')+scale\u fill\u手册(values=colforplots)+…
    这应该可以很好地工作!我没有意识到alpha参数可以作为一种美学包含在内!
    ggplot(EAtl, aes(x = New_categories, y = count, fill = New_categories, alpha = New_Water)) +
          geom_bar(position = 'stack', stat = 'identity', color = 'black')+
          scale_alpha_manual(values=c(0.8,0.4)) +
          scale_y_continuous(expand = c(0.01,0.01))+
          theme(panel.background = element_rect(fill = 'transparent', colour = NA),
                plot.background = element_rect(fill = 'transparent', colour = NA),
                panel.grid.minor = element_line(color = 'gray80'),
                panel.grid.major = element_line(color = 'gray80'),
                axis.title.x = element_blank(), 
                axis.text.x = element_blank(), axis.ticks.x = element_blank(),
                axis.text.y = element_text(size = 19, face = 'bold'), axis.title.y = element_blank())