how remove a point on the line chart (geom_line) R ggplot2?

Viewed 715

I'm new to R and I need your help. I need to remove the point number 8, x = "180" from multiple lines geom_line, but remaining at geom_point. What should you do?

Data is in an excel spreadsheet

data<-melt(CB_fechado, id.vars = 'a');
#Ângulo de incidência de vento
#print(data)

Grafico_CB_fechado <- ggplot(data,aes(x =`Ângulo de incidência de vento [°]`, y=`value`, color=`variable`))+
geom_line() + geom_point()+ 
scale_x_continuous(limits = c(0,180), breaks = c(0,15,30,45,60,75,90,105,120,135,150,165,180))+
scale_y_continuous(limits = c(-1.5,1.5))+ 
ylab("b")+theme(legend.position = "bottom")+
theme(legend.title = element_blank())

For exemplo

enter image description here

enter image description here

2 Answers

This is about subsetting the data that you use for the geom_line(). Not that this would be a bit more complex if it were not the last point. Here is an example with similar dummy data since I did not want to type in from the image.

dummy data

data <- data.frame(angle = rep(c(0:6*15, 180), 4),
                   cat = rep(LETTERS[1:4], each = 8),
                   value = rep(1:4/-4, each = 8))

subset data

Drop points with angle = 180.

data_lines <- data[data$angle != 180,]

graph it

Use data_lines instead of data in geom_line().

library(ggplot2)

ggplot(data, aes(x = angle, y = value, color= cat)) +
  geom_line(data = data_lines) +
  geom_point()+ 
  scale_x_continuous(limits = c(0,180), breaks = c(0,15,30,45,60,75,90,105,120,135,150,165,180))+
  scale_y_continuous(limits = c(-1.5,1.5))+ 
  ylab("b")+theme(legend.position = "bottom")+
  theme(legend.title = element_blank())

example ggplot2

The trick is to use a subset of the data in the ggplot call. In this case I use subset to remove the point with a = 180.
Note that I redefine the color argument to "red".

library(ggplot2)

ggplot(data, aes(x = a, y = b, color = "red")) +
  geom_point()+ 
  geom_line(data = subset(data, a != 180)) + 
  scale_x_continuous(limits = c(0,180), breaks = c(0,15,30,45,60,75,90,105,120,135,150,165,180))+
  scale_y_continuous(limits = c(-1.5,1.5))+ 
  ylab("b") +
  theme(legend.position = "bottom",
        legend.title = element_blank())

enter image description here

Data.

data <- read.table(text = "
    a       b
1   0   0.57395085
2   15  0.47593420
3   30  0.30175686
4   45  0.13363012
5   60  -0.02727459
6   75  -0.17971621
7   90  -0.44955122
8   180 -0.30247414
", header = TRUE)
Related