Reputation: 482
I have an app designed to plot 'pre-computed' data frames stored in a MYSQL database.
The data frames consist of four columns - id, genotype, A-value and B-value. The plot is a simple A/B geom_point, see example code below.
I want to manually define the shape and colour of the points based on the 'Genotype' variable.
The values for genotype will always be; "-1", "0" , "1" and "2". However, in some data frames, not all of these values are present (e.g. 1 only) however I would like to keep the shape/color the same between plots.
Does anybody have a solution for this?
library( ggplot2 )
# Example values
id <- c( "s1" , "s2" , "s3" , "s4" )
genotype <- c( -1 , 0 , 1 , 2 )
A_value <- c( 100 , 110 , 120 , 130 )
B_value <- c( 130, 120 , 110 , 100 )
# data_frame
df <- data.frame( id, genotype , A_value , B_value )
# Simple plot
ggplot(df, aes( A_value , B_value )) +
geom_point( aes(shape = factor( df$genotype ) , color = factor(df$genotype) ))
Upvotes: 0
Views: 759
Reputation: 7724
You can specify the levels of genotype and then use drop = FALSE
in the diescrete shape and color scales. Note that you should not use df$genotype
in the specification of aes
in the geom_point
-call as you are referring to the same dataframe.
ggplot(df, aes(A_value, B_value)) +
geom_point(aes(shape = factor(genotype, levels = c(-1 , 0 , 1 , 2)),
color = factor(genotype, levels = c(-1 , 0 , 1 , 2)))) +
scale_color_discrete(name = "genotype", drop = F) +
scale_shape_discrete(name = "genotype", drop = F)
ggplot(df[1:3, ], aes(A_value, B_value)) +
geom_point(aes(shape = factor(genotype, levels = c(-1 , 0 , 1 , 2)),
color = factor(genotype, levels = c(-1 , 0 , 1 , 2)))) +
scale_color_discrete(name = "genotype", drop = F) +
scale_shape_discrete(name = "genotype", drop = F)
Upvotes: 1