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

Label grouped bar plot in R

$
0
0

I'm tryng to add label to a grouped bar plot in r. However I'm using percentege in the y axis, and I want the label to be count. I've tried to use the geom_text() function, but I don't how exacly the parameters i need to use.


newdf3 %>%
  dplyr::count(key, value) %>%
  dplyr::group_by(key) %>%
  dplyr::mutate(p = n / sum(n)) %>%
  ggplot() + 
  geom_bar(
    mapping = aes(x = key, y = p, fill = value),
    stat = "identity",
    position = position_dodge()
  ) + 
  scale_y_continuous(labels = scales::percent_format(),limits=c(0,1))+
  labs(x = "", y = "%",title="")+ 
  scale_fill_manual(values = c('Before' = "deepskyblue", 'During' = "indianred1", 'After' = "green2", '?'= "mediumorchid3"),
                    drop = FALSE, name="")

plot1

Here is an exemple of how I need it:

plot2

here's a sample of data I'm using:

key   value

A     Before
A     After
A     During
B     Before
B     Before
C     After
D     During
...

I also wanted to keep the bars with no value (label = 0).

Can someone help me with this?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



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