How to add colors and linetype with ggplot2

ggplot2
#1

Hello dears,

I'm trying to control linetypes and colours of lines in a plot, but without sucess.
I want that the linetype and colour appear in the legend, but until now I only can did it to linetype. Follow my code below:

    ggplot()+
  geom_line(data=original_spectro, aes(x=Comprimento_de_onda_nm, y=Ponto_01,
                                       linetype="Point 01"), size=1.2)+
  geom_line(data=original_spectro, aes(x=Comprimento_de_onda_nm, y=Ponto_02,
                                       linetype="Point 02"), size=1.2)+
  geom_line(data=original_spectro, aes(x=Comprimento_de_onda_nm, y=Ponto_03,
                                       linetype="Point 03"), size=1.2)+
  geom_line(data=original_spectro, aes(x=Comprimento_de_onda_nm, y=Ponto_04,
                                       linetype="Point 04"), size=1.2)+
  xlab('Wavelength (nm)')+ylab('Reflectance (dimensionless)')+
  ggtitle("Reflectance curve", subtitle = "November, 2017")+
  theme(
    plot.title=element_text(hjust=0.5, face='bold', size = 14),
    plot.subtitle = element_text(hjust=0.5, size = 12)
  )+
  theme(legend.key.width=unit(2,"cm"),legend.key.height=unit(0.5,"cm"),
        legend.position = c(0.85, 0.75))+
  labs(linetype = "Sampling points")

0 Likes

#2

This would be easier to do if you reshape your data to a long format, could you share a sample of your data and turn this into a reproducible example?

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

#3

The difficulty with your data is that the reflectance data for each point are in separate columns
of your dataframe. As @andresrcs mentioned, you need to reshape your data to a "long" format,
where all the reflectance values are in a single column, and there is a new column describing which
point the values come from. Here's an example with some canned data:

library("tidyverse")
# get some spectral data
data("flowers", package = "pavo")
flowers <- flowers[,1:5] 
# pivot wide to long
long_flowers <- gather(flowers, key = "Species", value = "Reflectance", 2:5)
ggplot(data = long_flowers,
       mapping = aes(x = wl, y = Reflectance)) + 
  geom_line(mapping = aes(linetype = Species, color = Species))

With your code it would look like (caveat: I can't test if this works!)

os2 <- tidyr::gather(original_spectro, key = "Point", value = "Reflectance", Ponto_01, Ponto_02, Ponto_03, Ponto_04)
ggplot()+
  geom_line(data=os2, aes(x=Comprimento_de_onda_nm, y=Reflectance, linetype = Point, color = Point), size = 1.2) +
  xlab('Wavelength (nm)')+ylab('Reflectance (dimensionless)')+
  ggtitle("Reflectance curve", subtitle = "November, 2017")+
  theme(
    plot.title=element_text(hjust=0.5, face='bold', size = 14),
    plot.subtitle = element_text(hjust=0.5, size = 12)
  )+
  theme(legend.key.width=unit(2,"cm"),legend.key.height=unit(0.5,"cm"),
        legend.position = c(0.85, 0.75))+
  labs(linetype = "Sampling points")

Created on 2019-04-14 by the reprex
package
(v0.2.0).

2 Likes

#4

Dear, I got. @andresrcs has mentioned about the library 'reshape', and I have seen that library 'tidyr' is an appremorated version. Look below my code:

#install.packages('tidyr')
library(tidyr)
library(ggplot2)

help(tidyr)
original_spectro <- read.table(file='medias_p1ap4.txt',
                               header = TRUE)

os2 <- tidyr::gather(original_spectro, key = "Point", value = "Reflectance", Point_1, Point_2, Point_3, Point_4)
os2
ggplot()+
  geom_line(data=os2, aes(x=Comprimento_de_onda_nm, y=Reflectance, linetype = Point, color = Point), size = 1) +
  xlab('Wavelength (nm)')+ylab('Reflectance (dimensionless)')+
  ggtitle("Reflectance curve", subtitle = "November 28th, 2017")+
  theme(
    plot.title=element_text(hjust=0.5, face='bold', size = 14),
    plot.subtitle = element_text(hjust=0.5, size = 12)
  )+
  theme(legend.key.width=unit(2,"cm"),legend.key.height=unit(0.5,"cm"),
        legend.position = c(0.85, 0.75))+
  scale_colour_manual(values = c("darkblue", "black", "darkgreen", "red"))+
  labs(linetype="Sampling points", colour="Sampling points")

ggsave("original_spectro.jpeg", width = 15, height = 10, units = "cm", dpi = 1500)

Rplot

Thank you guys! You two have soluctioned my problem!

1 Like

#5

Congratulations for solving your problem!, just a little comment for clarification, I wasn't talking about the reshape package I was talking about literally "changing the shape of your dataset", and Drew has gave an example of that using the tidyr package.

Anyways, I'm glad you have solved your problem, so, if your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

closed #6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes