Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re-sizing ggplot geom_dotplot

Tags:

r

ggplot2

I'm having trouble creating a figure with ggplot2. I am using geom_dotplot with center stacking to display my data which are discrete values for 4 categories.

For aesthetic reasons I want to customize the positions of the dots so that

  1. reduce the empty space between dots along the y axis, (ie the dots are 1 value large)
  2. The distributions fit and don't overlap

I've adjusted the bin and dotsize to achieve aesthetic goal 1, but that requires me to fiddle with the ylim() parameter to make sure that the groups fit in the plot. This results in a plot with more whitw space and few numbers on the y axis.

Question: Can anyone explain a way to resize the empty space on this plot?

My code is below:.

plot <- ggplot(figdata, aes(y=Counts, x=category, col=strain)) + 
            geom_dotplot(aes(fill=strain), dotsize=1, binwidth=.7, 
                         binaxis= "y",stackdir ="centerwhole", stackratio=.7) +
            ylim(18,59)

plot + scale_color_manual(values=c("#E69F00", "#56B4E9")) + 
       geom_errorbar(stat="hline", yintercept="mean", 
                     aes( ymax=..y..,ymin=..y.., group = category, width = 0.5), 
                     color="black")

Which produces:

enter image description here

EDIT: Incorporating jitter will allow the all the data to fit, but I don't want to add noise to this data and would prefer to show it as discreet data. adjusting the binwidth and dotsize to 0.3 as suggested below also fits all the data, however it leaves too much white space.

enter image description here

I think that I might have to transform my data so that the values are steps smaller than 1, in order to get everything to fit horizontally and dot sizes to big large enough to reduce white space.

like image 808
user1757654 Avatar asked Oct 27 '25 23:10

user1757654


1 Answers

I think the easiest way is using coord_cartesian:

plot + scale_color_manual(values=c("#E69F00", "#56B4E9")) + 
  geom_errorbar(stat="hline", yintercept="mean", 
                aes( ymax=..y..,ymin=..y.., group = category, width = 0.5), 
                color="black") +
  coord_cartesian(ylim=c(17,40))

Which gives me this plot (with fake data that are not as neatly distributed as yours): enter image description here

like image 122
RHA Avatar answered Oct 30 '25 14:10

RHA



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!