0
votes

I'm trying to do a plot which consists in two main parts, the "background" is the shape of a USA state and on top, I'm adding measurement points (using latitude and longitude coordinates) which I want to be color scaled according to the value of the measurement (The data comes from a data frame). I'm having a hard time changing the color of the points and personalizing the legend bar, I would like the bar to also show the max and minimum values and use a color scale that is more visually appealing.

    m = map_data('state', region = state)

finalplot <- ggplot() + 
  geom_polygon( data=m, aes(x=long, y=lat), colour="black", fill="white" ) +
  geom_point(data=filteredtable,aes(x=LongitudeMeasure,y=LatitudeMeasure, colour = Result)) +
  ggtitle(paste0("Measurement points of ", contaminant, " in ", state)) +
  theme_void() 

when adding something like + scale_color_grey(start = 0.8, end = 0.2) it gives me the following Error: Continuous value supplied to discrete scale

If you have any other idea in what would be the best approach into doing this type of plot I would appreciate it.

1

1 Answers

0
votes

I think this is a good example of why it's better to post some data in your question as well as showing us your code. However, it's possible to create some data so that your exact plotting code produces a reasonable output:

set.seed(69)
filteredtable <- data.frame(LongitudeMeasure = runif(100, -81.5, -80.5), 
                             LatitudeMeasure = runif(100, 26, 28),
                             Result = runif(100))
state       <- "Florida"
contaminant <- "Dilithium"

Now let's try your plotting code:

m = map_data('state', region = state)

finalplot <- ggplot() + 
  geom_polygon( data=m, aes(x=long, y=lat), colour="black", fill="white" ) +
  geom_point(data=filteredtable,aes(x=LongitudeMeasure,y=LatitudeMeasure, colour = Result)) +
  ggtitle(paste0("Measurement points of ", contaminant, " in ", state)) +
  theme_void()

So our plot looks like this:

finalplot

enter image description here

But if we try to add the grayscale that you wanted, we get the same error:

finalplot + scale_color_grey(start = 0.8, end = 0.2)
#> Error: Continuous value supplied to discrete scale

The reason for this is that scale_color_grey produces a discrete gray color scale, but you want a continuous color scale, since you have a continuous variable for Result. You probably wanted scale_color_gradient or scale_color_gradientn. Let's try scale_color_gradient with a grayscale palette and set our breaks to 0.1 increments so we get the labels we want on the bar:

finalplot + scale_color_gradient(low = "gray20", high = "gray80", breaks = seq(0, 1, 0.1))

enter image description here

Or if we want something more colorful:

finalplot +
  scale_color_gradientn(colours = c("red", "gold", "forestgreen"), breaks = seq(0, 1, 0.1))

enter image description here