Martin S
Martin S

Reputation: 397

Using one color scale for two lines in R with ggplot2

I have min and max temperature data which I want to colorize properly.

The data looks like this

 MESS_DATUM  LUFTTEMPERATUR_MAXIMUM LUFTTEMPERATUR_MINIMUM
20050719       22.1    16.5
20050720       22.8    12.8
20050721       22.6    12.0
20050722       19.1    13.8
20050723       21.9    12.8
20050724       24.8    11.4
20050725       25.8    16.9
20050726       24.7    16.3
20050727       31.6    17.0
20050728       34.5    19.2
20050729       27.3    18.2
20050730       25.5    13.4
20050731       24.5    11.5
20050801       24.4    12.5
20050802       22.1    14.6
20050803       24.9    15.2
20050804       23.4    11.3
20050805       23.4     9.1
20050806       21.0    12.0
20050807       19.2     9.8
20050808       19.2    10.8
20050809       21.0    11.2
20050810       20.2    10.2

Those two lines get seperate colors spectrums but I want the same spectrum used for both lines. This is the code I used for that.

  p2<-ggplot(dataByYear) +
geom_line(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM, colour = LUFTTEMPERATUR_MINIMUM)) +
geom_smooth(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM), color = "blue",size = 0.5) +
geom_line(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM, colour = LUFTTEMPERATUR_MINIMUM)) +
geom_smooth(aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM), color = "red",size = 1) +
scale_colour_gradient2(low = "blue", mid = "green" , high = "red", midpoint = 10) +
scale_y_continuous(limits = c(-10,40), breaks = seq(-10,40,5)) +
ggtitle ("Daily average temperature") +
xlab("Date") +  ylab ("Average Temperature ( ºC )")

It looks like this

enter image description here

As you can see the color is not unique for a certain temperature. It can be red on the bottom line for values greater 15°C but still green for the same temperature on the top line.

I want a color scale that is used for both lines equally. Does anybody have an idea how to do that?

Thanks in advance.

Upvotes: 1

Views: 156

Answers (2)

Martin S
Martin S

Reputation: 397

Update 2 hours later ...

Ok, I found out what I need to do.

My problem was that my data frame has even more columns which should not be merged by tidyr.gather(). I had to read the documentation of tidyr.gather() but then I figured out that I need to create a seperate data frame with only those 3 columns. Then I could merge them and use them in the plot.

The solution for me was first to create a separate data frame and then to adapt the variable names in the plot command.

#Prepare data frame for ggplot
df <-
data.frame(
  dataByYear$MESS_DATUM,
  dataByYear$LUFTTEMPERATUR_MAXIMUM,
  dataByYear$LUFTTEMPERATUR_MINIMUM
)

#Plot data
#tidyr::gather creates Key/Values pairs temp/Temperatures which are used to
#diplay the values equally colored
p2 <-
 ggplot(
  tidyr::gather(df, temp, Temperature,-dataByYear.MESS_DATUM),
  aes(x = dataByYear.MESS_DATUM, y = Temperature, colour = Temperature)
) +
geom_line(aes(group = temp)) +
geom_smooth(
  data = df,
  aes(x = dataByYear.MESS_DATUM, y = dataByYear.LUFTTEMPERATUR_MINIMUM),
  color = "blue",
  size = 0.5
) +
geom_smooth(
  data = df,
  aes(x = dataByYear.MESS_DATUM, y = dataByYear.LUFTTEMPERATUR_MAXIMUM),
  color = "red",
  size = 1
) +
scale_colour_gradient2(
  low = "blue",
  mid = "green" ,
  high = "red",
  midpoint = 10
) +
scale_y_continuous(limits = c(-10, 40), breaks = seq(-10, 40, 5)) +
ggtitle ("Daily average temperature") +
xlab("Date") +  ylab ("Average Temperature ( ºC )") +
scale_x_date(date_breaks = "1 month", date_labels = "%b")

Now, that is my final result. Looks nice.

enter image description here

Upvotes: 1

tonytonov
tonytonov

Reputation: 25608

Welcome to SO! Here's a quick workaround: additionally use group on melted data. Don't mind the broken date, I didn't bother to fix it, with your data frame everything should be good. Still, you can see the common color scale as desired.

ggplot(tidyr::gather(df, temp, value, -MESS_DATUM), 
       aes(x = MESS_DATUM, y = value, colour = value)) +
  geom_line(aes(group = temp)) +
  geom_smooth(data = df, aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MINIMUM), color = "blue",size = 0.5) +
  geom_smooth(data = df, aes(x = MESS_DATUM,y = LUFTTEMPERATUR_MAXIMUM), color = "red",size = 1) +
  scale_colour_gradient2(low = "blue", mid = "green" , high = "red", midpoint = 10) +
  scale_y_continuous(limits = c(-10,40), breaks = seq(-10,40,5)) +
  ggtitle ("Daily average temperature") +
  xlab("Date") +  ylab ("Average Temperature ( ºC )")

enter image description here

Upvotes: 2

Related Questions