Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Putting the legend inside the graph

$
0
0

I am trying to plot a stacked barchart of the global terorrism dataset available at the following link: https://www.kaggle.com/START-UMD/gtd My aim is to put the legend inside the graph, precisely in the bottom right, and to increase the total size of the plot in order to make it readable. The closest thing to my intention that I managed to reach get is the made up by the following code.

Library necessary for this plot

library(tidyverse)
library(ggplot)

Code:

terrorism %>% 
  group_by(weaptype1_txt, weapsubtype1_txt) %>% 
  summarise(nweapons = length(weaptype1_txt)) %>% 
  ungroup() -> dfw2

ggplot(data = dfw2, aes(x = reorder(weaptype1_txt,nweapons), y = nweapons, fill = weapsubtype1_txt)) +  
  geom_bar(aes(fill=weapsubtype1_txt), stat="identity", colour="white") +
  coord_flip() +
  theme_bw(base_size = 8)  +
   labs(title="Weapons type/subtype (1)", x ="Type of weapon used", y = "Number of events", fill = "Sub-type") + 
  theme(legend.position=c(0.8, 0.2))+
  theme(legend.title = element_text(size = 3), 
               legend.text = element_text(size = 3))

enter image description here

My intention is of course to not cover bars with the legend. Thank you very much for reading and, I hope, for the help.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>