Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Barplot with outline per unit?

Tags:

r

ggplot2

I want a barplot that outlines per unit in the y-axis. For example, the plot below using the iris dataset will plot a smooth barplot, however what I want is to outline each unit. I drew in the box to illustrate.

library(ggplot2) 
freqtable <- table(iris$Petal.Width)
df <- as.data.frame.table(freqtable)
colnames ( df ) = c( "Width", "Freq")
df$Width = as.numeric ( as.character ( df$Width ) )


ggplot(df[ df$Width > 2.2, ] , aes( Width , Freq)) +
  geom_bar(position="stack", stat="identity", fill="tomato2")

enter image description here

like image 846
Ahdee Avatar asked Feb 10 '26 18:02

Ahdee


1 Answers

A bit of a hack but this does the job :

library(tidyverse)
df2 <- 
  uncount(df[ df$Width > 2.2, ] , Freq) %>%
  mutate(Freq=1)
  

ggplot(df2, aes( Width , Freq)) +
  geom_bar(position="stack", stat="identity", fill="tomato2", color = "black")

Created on 2021-09-12 by the reprex package (v2.0.1)

like image 168
Moody_Mudskipper Avatar answered Feb 12 '26 15:02

Moody_Mudskipper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!