Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lines overlaid on ggplot2 bar plot when saved with ggsave()

Tags:

r

ggplot2

When I view this bar plot in R Studio, it appears as I intended (this is from a screenshot): intended plot

However, when I use the ggsave("filename.png") function, it appears with light-colored lines overlaid (may have to look closely to see):

saved plot with lines overlaid

I'm using R version 3.2.3, ggplot2 version 2.00, and R Studio version 0.99.486, on OS X 10.11.3.

Why might this be happening?

like image 702
Joshua Rosenberg Avatar asked Sep 10 '25 01:09

Joshua Rosenberg


2 Answers

You should check out the Cairo library. I use it for crisp graphics in presentations and reports.

Cairo initializes a new graphics device that uses the cairo graphics library for rendering. The current implementation produces high-quality PNG, JPEG, TIFF bitmap files, high resolution PDF files with embedded fonts, SVG graphics and PostScript files. It also provides X11 and Windows interactive graphics devices. Unlike other devices it supports all graphics features including alpha blending, anti-aliasing etc.

I can't reproduce your example, but here's a similar one.

library("ggplot2")
pl <- ggplot(aggregate(mpg ~ cyl, mtcars, FUN=mean), 
             aes(x = cyl, y = mpg)) + 
       geom_bar(stat="identity", fill="red3") +
       theme_bw()

library("Cairo")
CairoPNG("CairoCarPlot.png")
pl
dev.off()

Uploading the PNG, it looks like: enter image description here

like image 124
C8H10N4O2 Avatar answered Sep 12 '25 15:09

C8H10N4O2


Throwing this out there in case it helps anyone else. I had this same issue when the stacked bar chart was made up of multiple values, I fixed it by grouping and then summarizing. I think for your data it would be:

df <- dataframe %>% group_by(Weekday) %>% summarize(percent=sum(percentage of tweets))
like image 35
Ella Avatar answered Sep 12 '25 15:09

Ella