0

I am new to ggplot2 - until now I always used the R base plot functions. Anyway, I have some data that I was able to plot, but I'm not quite satisfied with the result yet. Here's some code to reproduce the plot:

library( ggplot2)
library( ggstance)
method <- rep( c( 'a', 'b', 'c', 'd', 'e', 'f'), 6)        
scheme <- c( rep( 'sc1', 18), rep( 'sc2', 18))
classif <- runif( 36, min = 0.4, max = 0.6)
para <- c( rep( 0.1, 6), rep( 0.2, 6), rep( 0.3, 6), rep( 0.4, 6), rep( 0.5, 6), rep( 0.6, 6))
five <- runif( 36, max = .3)
ninetyfive <- runif( 36, min = .7)
avg <- ninetyfive - five
dat <- data.frame( method, scheme, classif, para, five, ninetyfive, avg)
dat[ c( 'para', 'method', 'scheme')] <- lapply( dat[ c( 'para', 'method', 'scheme')], FUN = as.factor)
ggplot( dat, aes( x = classif, y = method)) + 
  labs( x = 'Number of features', y = 'method') + 
  geom_point() + 
  geom_errorbarh( aes( xmin = five, xmax = ninetyfive)) + 
  facet_wrap( vars( para, scheme))

The result looks like this:

enter image description here

I want to amend two things: First, and most important, I want to add the avg column to my plot with a different symbol/colour. Second, I'd love to amend the text in each of those boxes ( the ones that contain 0.1, 0.2, ..., 0.6) and I would also like to change the text sc1 and sc2 to something different - but I don't want to change it in the data-frame. Any ideas?

1 Answers1

1

To change symbol/color of any geom in ggplot, you modify its aesthetics. In general, ggplot's reference site is a great source for information about ggplot. In your case, you simply modify add another geom_point() with avg, and modify its aesthetics as follows:

ggplot( dat, aes( x = classif, y = method)) + 
  labs( x = 'Number of features', y = 'method') + 
  geom_errorbarh( aes( xmin = five, xmax = ninetyfive)) + 
  geom_point(aes(x = classif,y = method)) + 
  geom_point(aes(x = avg,y = method), shape = 2, color = "red") +
  facet_wrap( vars( para, scheme))

You can change shape/color/alpha/etc by checking out the aesthetic options for geom_point.

As for changing the labels of facet_wrap without changing the data-frame, check out this particular answer of a previous question: https://stackoverflow.com/a/12104207/9985527.

doubled
  • 348
  • 3
  • 12