Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making ggplot look as nice as the native plot in R

I have started using ggplot because I heard it's a lot more flexible and looks a lot better than the native plot function. However, my results ggplot graph looks worse than plot function so I must be doing something wrong. For example, the labels are too small to be legible, the line does not have any points on it, and the ratio just looks better with the default plot function. I am new to data visualization, so any guide or suggestions to making the graph look better would be much appreciated.

With plot:

plot(table(month(data$DATE)), type="b", 
  main="Time vs. Freq", 
  xaxt='n',
  xlab="Month",
  ylab="Frequency")
axis(1, at=1:9, labels = month.name[1:9])

with plot

With ggplot:

x <- month(data$DATE)
df = data.frame(x)
df$y <- 1
ggplot(df, aes(x, y)) + stat_summary(fun.y = sum, geom = "line") + xlab("Month") + ylab("Freq") + ggtitle("Time vs. Freq")

enter image description here

like image 312
THIS USER NEEDS HELP Avatar asked Nov 24 '25 18:11

THIS USER NEEDS HELP


1 Answers

It's not completely clear what you don't like about the default ggplot2 plots but have you tried one of the other themes?

p <- ggplot(df, aes(x, y)) + stat_summary(fun.y = sum, geom = "line") + 
            xlab("Month") + ylab("Freq") + ggtitle("Time vs. Freq")
p + theme_bw() # For black/white publications plots

Or grab more themes and experience

install.packages("ggthemes") 
library(ggthemes) 
p + theme_tufte()      # Based on Tufte's ideas
p + theme_stata()      # Resembles plots from stata
p + theme_economist()  # A la plots in the economist

just to show a few examples. And they can be tweaked as you please

like image 106
ekstroem Avatar answered Nov 26 '25 08:11

ekstroem