Home > database >  ggplot: edit shape of points based on second column
ggplot: edit shape of points based on second column

Time:10-14

My goal is to plot a map with dwelling locations as points, where points are divided into two colours, based on a categorical variable, name category. Of those dwellings, a few dwellings need to have a different shape, e.g., a star. The column that describes this is called star in the example below. My dataframe looks like this:

x y category star
123 456 1 0
143 556 0 0
124 556 1 1
233 256 1 0
ggplot(data = df, aes(x = x, y = y, color=category))  
  geom_point()  

The code above gives me what I need, except for the 'stars'. How can distinguish this second column?

CodePudding user response:

Have assumed you want the points with star with a value of 1 to be a star shape.

library(ggplot2)

ggplot(data = df1, aes(x = x, y = y, color=factor(category), shape = factor(star)))  
  geom_point(size = 8)  
  scale_shape_manual(breaks = c(0, 1),
                     values = c(1, 11)) 
  labs(color = "Category",
       shape = "Star")

data

df1 <- structure(list(x = c(123L, 143L, 124L, 233L), 
                      y = c(456L, 556L, 556L, 256L), 
                      category = c(1L, 0L, 1L, 1L), 
                      star = c(0L, 0L, 1L, 0L)), 
                 class = "data.frame", row.names = c(NA, -4L))

Created on 2022-10-13 with reprex v2.0.2

  • Related