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)) 嗨,我有上面的条形图的代码和模式填充。见下面关联的图像。这是我从堆栈溢出的不同来源拼凑而成的代码,我很高兴它终于起作用了,因为我现在终于有了五个条,每条都有一个不同的填充模式。然而,很明显,这些模式并不一定与特定的电影相关联(电影有五个价值)。我想知道如何才能将模式填充到特定的电影中,因为目前这个传说无法显示,而且我认为正是因为这个(也就是说,事情没有被正确地映射?)如有任何建议,将不胜感激。马洛

补充一句,我发现这个在线https://evamaerey.github.io/flipbooks/ggpattern/ggpattern#28可以做我想做的事情,但是看起来不像上面的那个那么好。这现在可以,但如果有人有任何建议,如何将两者结合起来,或如何调整上述代码,以便我可以得到关键的显示,这将是非常感谢的!谢谢!
发布于 2022-04-30 15:09:32
当您在aes之外设置东西时,就像在传奇之外设置它们一样。
如果有什么东西,比如你愿意失去控制的角度或模式,你可以把它变成一个传奇。例如:
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))

您可能已经注意到我使用了geom_col_pattern()。
我看到您有aes(pattern = film) --这行什么都不做,它需要绑定到geom_或stat_调用。
如果设置了变量的显示方式,则可以使用scale_color_manual和scale_fill_manual调用来完成此操作。
例如:
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))

https://stackoverflow.com/questions/72065400
复制相似问题