1. error message

1. Title 

... + ggtitle('title') # or labs(title = 'title', ...)

# title appears at topright by default. To move title to center

... + theme(plot.title = element_text(hjust = 0.5))


2. labels

... + xlabs('label') + ylabs('label') # or labs(x = 'xlab', y = 'ylab')


3. line

... + geom_line(size = 2, color = '') # just connect points with line

... + geom_abline(size = 2, color = '') # draw regression line

... + geom_vline(size = 2, color = '') # draw vertical line

... + geom_hline(size = 2, color = '') # draw horizontal line


4. draw dynamic plot with manipulate function

myplot <- function(dynamic variable){

d <- data.frame(..., temp = dynamic variable, ...),

g <- ggplot(d, aes(...))

g <- g + geom_line()

...

g

}

manipulate(myplot(dynamic variable), slider(min, max, step = 1))






1. coloring manual

1) geom_point, geom_line, etc.

grouping with color for discrete value

g <- ggplot(data = df)

g + geom_point(aes(x = var1,  y = var2, color = 'discrete variable name')) +

      geom_line(aes(x= var1, y = var2, color = 'discrete variable name'))+ 

      scale_color_manual('legend title', breaks = 'discrete variable name', values = 'color value')

# when drawing more than one point or line, breaks = c(), values = c()


grouping with color for continuous value

g <- ggplot(data = df)

g + geom_point(aes(x = var1,  y = var2, color = 'continuous variable name')) +

      geom_line(aes(x= var1, y = var2, color = 'continuous variable name'))+ 

      scale_color_continuous('legend title', low = 'color for low value', high = 'color for high value')

# scale_color_continuous can be exchanged with scale_color_gradient or scale_color_gradientn( n step gradient )


2) geom_bar, geom_hist, etc.

# almost same as above

... + scale_fill_manual('legend title', breaks = 'discrete variable name', values = 'color value')

# for coloring with discrete variable

... + scale_fill_continuous('legend title', low = 'color for low value', high = 'color for high value')

# for coloring with continuous variable


+ Recent posts