So I am trying to visualize my data in R using the ggpattern package. The problem is that I can't seem to edit the legend's title using the guides function. Before I go further into that, let me show the data
tag Center n n_tag persen
1 lisan же 11 2142 0.005135387
2 medrus же 6 2588 0.002318393
3 medsos же 14 2778 0.005039597
4 puisi же 15 2622 0.005720824
5 lisan ну 39 2142 0.018207283
6 puisi ну 13 2622 0.004958047
And this is the visualization, using the ggpattern package:
As you can see, the title for the legend is currently "Center". I want to change it into "Legenda".
So far, this is the code that I have tried of using, but to no avail:
judul <- paste("Sebaran leksikon", kata_difilter, "\n", "dalam Korpus Nasional Rusia")
subjudul <- paste("Jumlah token: ", ftj)
viz_akhir <- ggplot(df_viz, aes(tag, persen, fill=Center)) +
geom_bar_pattern(stat="identity",
position=position_dodge(),
pattern_color = NA,
pattern_fill = "black",
pattern_density = 0.25,
pattern_spacing = 0.010,
pattern_key_scale_factor = 0.70,
aes(pattern=Center)) +
scale_fill_grey(start = 0, end = .9) + #untuk b&w
theme_light() +
labs(title=judul, subtitle=subjudul) +
xlab("Tag korpus") + ylab("Persentase dalam korpus") +
guides(pattern=guides_legend(title="Legenda"))
theme(plot.title=element_text(hjust=0.5, face="bold", size=15),
plot.subtitle=element_text(hjust=0.5, size=8))
print(viz_akhir)
This is the visualization of that code:
It instead make a new legend in place of changing the legend from the ggpattern visualization.
Anybody knows the solution? Thanks in advance!
You have both the fill
and the pattern
aesthetic mapped to the same variable, so you can combine the legends simply by giving these aesthetics the same names.
You didn't include kata_difilter
or ftj
in your question, so the plot is not fully reproducible, but I have recreated a modified version of judul
and subjudul
so the following code should work for you:
ggplot(df_viz, aes(tag, persen, fill = Center)) +
geom_bar_pattern(stat = "identity",
position = position_dodge(),
pattern_color = NA,
pattern_fill = "black",
pattern_density = 0.25,
pattern_spacing = 0.010,
pattern_key_scale_factor = 0.70,
aes(pattern = Center)) +
scale_fill_grey(start = 0, end = 0.9) +
theme_light() +
labs(title = judul,
subtitle = subjudul,
x = "Tag korpus",
y = "Persentase dalam korpus",
pattern = "Legenda",
fill = "Legenda") +
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 15),
plot.subtitle = element_text(hjust = 0.5, size = 8))