Lines with alternate colors for a category variable ggplot

Asked

Viewed 592 times

1

I have a database with ration per animal per day.

Just follow one part of the base to get an idea:

Animal	Time	Consumo
5	1	2.53396
5	2	2.32906
5	3	2.94379
5	4	3.36162
6	1	2.89082
6	2	2.53898
6	3	2.97881
6	4	3.03876
7	1	2.81885
7	2	2.73889
7	3	2.67891
7	4	2.87885

I want to generate a chart in which only one of the animals (Ex: Animal=7) has different line color (consumption in the team function) and the others have the same color. In ggplot I managed to make a chart so that all animals have the same color, I just can’t make a line for animal 7 with different color. Follow the line I used:

library(ggplot2)
Consumo$Animal <- factor(Consumo$Animal)
ggplot(data = Consumo, aes(x = Time, y = Consumo, colours=Animal)) +
  geom_line(color="grey") +
  xlab("Time, d") +
  ylab("Forecast daily feed intake,kg") +  
  theme_bw() +
  xlim(c(5, 80)) + 
  ylim(c(0, 5))+
  labs(title = "C: Current model") + theme(plot.title = element_text(face="bold",size=18, hjust = 0))+
  theme(axis.title = element_text(size = 18),
        axis.text = element_text(size = 14))
Below is the chart I generated with this command inserir a descrição da imagem aqui

1 answer

1


Initial data

texto <- 'Animal    Time    Consumo
5   1   2.53396
5   2   2.32906
5   3   2.94379
5   4   3.36162
6   1   2.89082
6   2   2.53898
6   3   2.97881
6   4   3.03876
7   1   2.81885
7   2   2.73889
7   3   2.67891
7   4   2.87885'

Consumo <- read.table(text = texto, header = TRUE)
Consumo$Animal <- factor(Consumo$Animal)

Building the graph

A simple way to highlight a particular animal, say "7", is to create a variable that indicates whether that observation belongs to the group that will be highlighted or not. It could be something simple like:

Consumo$especial <- ifelse(Consumo$Animal == '7', 'especial', 'normal')

And then map the color in this variable.

library(ggplot2)
gg1 <- ggplot(data = Consumo, aes(x = Time, y = Consumo, colours=Animal)) +
  geom_line(aes(color = especial))
gg1

gráfico básico

To turn all cases gray and highlight only the special group just add scale_color_manual(). The function guides() removes the color caption.

gg2 <- gg1 +
  scale_color_manual(values = c(especial = 'red', normal = 'grey')) +
  guides(colour = 'none')
gg2

inserir a descrição da imagem aqui

Final graph

At the end the chart with the theme you wanted can be obtained with

gg2 + xlab("Time, d") +
  ylab("Forecast daily feed intake,kg") +  
  theme_bw() +
  labs(title = "C: Current model") + 
  theme(plot.title = element_text(face = "bold", size = 18, hjust = 0),
        axis.title = element_text(size = 18),
        axis.text = element_text(size = 14))

inserir a descrição da imagem aqui

OBS: I removed the boundary functions - xlim() and ylim() - of its theme because they made the data passed in the example disappear.

  • How could I include a new line in this same graph that represents the coefficient of variation between animals as a function of time (time)? The idea was to put in the second axis on this same chart. I its calculate the CV by dplyr. But I do not know how to enter in this chart

  • To calculate the CV, see here. And to include another axis see here.

  • It worked out to calculate the CV and generate the chart. However, Oce would know how to change the source to Times in the second axis that was created? All other letters are in Times, only in the second axis that this Arial

  • @Lucianohauschild Ask a new question sharing the code of your attempt and the expected result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.