ggplot(unique(films2[,c("film","word.len.avg")]) ,aes(film,word.len.avg,fill=film,))+
  geom_bar_pattern(stat="identity", 
                   pattern = 
                     
                     c(
                       "circle", 
                       "stripe",  
                       "none",  
                       "wave", 
                       "crosshatch" 
                     ),
                   pattern_angle = c(rep(45, ), 
                                     rep(60, ), 
                                     rep(45, ),
                                     rep(45, ),
                                     rep(45,)),
                   fill            = 'white',
                   colour          = 'black', 
                   pattern_density = .35, 
                   pattern_fill    = 'darkblue',
                   pattern_colour  = 'darkblue'
                   
  ) +
  scale_x_discrete(breaks = c("dial.m.for.murder", "pscyho", "rear.window", "rope", "vertigo"), 
                   labels = c("Dial M for Murder", "Psycho", "Rear Window", "Rope", "Vertigo"))+
  theme_bw() +
  aes(pattern = film)+
theme(legend.position = "right") + 
  coord_fixed(ratio = 1.5)+
  scale_pattern_spacing_discrete(range = c(0.01, 0.05)) 

Hi I have the above code for a bar graph with pattern fills. See associated image below. This is code I cobbled together from various sources on stack overflow, and I'm pleased it finally works in that I now finally have five bars with a different fill pattern in each. However, it's clear that these patterns are not necessarily associated with a specific film (there are five values for film). I'm wondering how I can get the pattern fills to be mapped to specific films, because at the moment the legend won't show, and I'm assuming it's because of this (i.e. things haven't been mapped properly?). Any advice would be greatly appreciated. Maroenter image description here

只是想补充一点,我在网上找到了这个https://evamaerey.github.io/flipbooks/ggpattern/ggpattern#28,它做了我想做的事情,但看起来不像上面的那个那么好.现在就可以了,但是如果有人对如何将两者结合起来,或者如何修改上面的代码,以便我能得到显示的密钥有任何建议,我也将不胜感激!非常感谢.

推荐答案

当你把事情设定在aes之外时,就像把它们设定在传奇之外.

如果有什么东西,如Angular 或图案,你愿意失go 控制,你可以把它变成一个传奇.例如:

ggplot(unique(films2[,c("film","word.len.avg")], aes(film, word.len.avg,)) + 
  geom_col_pattern(aes(pattern = film, fill = film, 
                       pattern_angle = film, pattern_spacing = film),
                   fill            = 'white',
                   colour          = 'black',
                   pattern_density = .35,
                   pattern_fill    = 'darkblue',
                   pattern_colour  = 'darkblue') +
  theme_bw() +
  theme(legend.position = "right") + 
  coord_fixed(ratio = 1.5)+
  scale_pattern_spacing_discrete(range = c(0.01, 0.05))

enter image description here

你可能已经注意到我用了geom_col_pattern().

我看到你有aes(pattern = film)个电话,这条线什么都没做,它需要连接到geom_stat_电话.

如果设置了变量的表示方式,可以使用

例如:

ggplot(unique(films2[,c("film","word.len.avg")], aes(film, word.len.avg,, fill = film))+
  geom_bar_pattern(stat="identity", 
                   pattern = c("circle", "stripe", "none",
                               "wave", "crosshatch" ),
                   pattern_angle = c(45, 60, rep(45, 3)),
                   # fill            = 'white',
                   colour          = 'black', 
                   pattern_density = .35, 
                   pattern_fill    = 'darkblue',
                   pattern_colour  = 'darkblue'
                   
  ) + scale_fill_manual(values = setNames(c("darkred", "darkblue", "white", 
                                                    "lightyellow", "gray"),
                                                  unlist(df$film))) +
  scale_x_discrete(breaks = c("dial.m.for.murder", "pscyho", 
                              "rear.window", "rope", "vertigo"), 
                   labels = c("Dial M for Murder", "Psycho", "
                              Rear Window", "Rope", "Vertigo"))+
  theme_bw() +
  # aes(pattern = film)+
  theme(legend.position = "right") + scale_pattern_fill_viridis_c() +
  coord_fixed(ratio = 1.5)+
  scale_pattern_spacing_discrete(range = c(0.01, 0.05))

enter image description here

R相关问答推荐

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

手动打印线型gplot

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

derrr mutate case_when grepl不能在R中正确返回值

在for循环中转换rabrame

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

R-按最接近午夜的时间进行筛选

基于Key->Value数据帧的基因子集相关性提取

基于数据集属性将科分配给物种

在具有多个响应变量的比例堆叠条形图上方添加总计

如何在R中使用混合GAM模型只对固定的影响因素进行适当的预测?

删除数据帧中特定行号之间的每第三行和第四行

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

使用ggplot2绘制具有边缘分布的坡度图

每行不同列上的行求和

基于R中的引用将向量值替换为数据框列的值

如何准确地指出Read_delim所面临的问题?

如何计算物种矩阵中一行中的唯一个数?

同时使用Scale_y_Break和Scale_x_Continue时,X轴值出现两次