Home > Blockchain >  Error in `f()`: ! Insufficient values in manual scale. 4 needed but only 0 provided
Error in `f()`: ! Insufficient values in manual scale. 4 needed but only 0 provided

Time:10-18

ggplot(data = house, 
       mapping = aes(x = GrLivArea,y = SalePrice)) 
  geom_point(size=2, shape = 16, mapping = aes(color = ifelse(YearBuilt<=1908,"purple",    
                  ifelse(YearBuilt>1908 & YearBuilt<=1943,"orange", 
                         ifelse(YearBuilt>1943 & YearBuilt<= 1978,"green", 
                                    ifelse(YearBuilt>1978,"red", "white")))))) 
   scale_color_manual(labels = c("1872-1908", "1909 - 19043", "1944 - 1978", "1979 - 2010"))  
  labs(color = "Year Built")

error = Error in f(): ! Insufficient values in manual scale. 4 needed but only 0 provided.

CodePudding user response:

from the documentation, scale_color_manual() requires the argument "values" to be set:

a set of aesthetic values to map data values to. The values will be matched in order (usually alphabetical) with the limits of the scale, or with breaks if provided. If this is a named vector, then the values will be matched based on the names instead. Data values that don't match will be given na.value.

    p <- ggplot(mtcars, aes(mpg, wt))  
geom_point(aes(colour = factor(cyl)))
p   scale_colour_manual(values = c("red", "blue", "green"))

I think you'd see best results by switching your current approach. So create a column with the labels, and then use color manual to tell which colors to use.

    ggplot(data = house, 
       mapping = aes(x = GrLivArea,y = SalePrice))  
geom_point(size=2, shape = 16, mapping = aes(color = ifelse(YearBuilt<=1908,"1872-1908",    
                  ifelse(YearBuilt>1908 & YearBuilt<=1943,"1909 - 19043", 
                         ifelse(YearBuilt>1943 & YearBuilt<= 1978,"1944 - 1978", 
                                    ifelse(YearBuilt>1978,"1979 - 2010", "other")))))) 
   scale_color_manual(values = c('purple', 'orange', 'green', 'red', 'white'))  
  labs(color = "Year Built")

It also might be better for you to create that column outside of the call to ggplot.

CodePudding user response:

Unlike base graphics, you don't need a vector of color names as long as your data in ggplot. Use the values of scale_color_manual instead. (As of fairly recently, you can do it with scale_color_identity, but it's not the standard way to use it.)

I'd also recommend just adding your binned column wiht desired labels to the data rather than doing that inside the plot:

house$age_category = with(house, ifelse(
  YearBuilt <= 1908, "1872-1908", ifelse(
    YearBuilt <= 1943, "1909-1943", ifelse(
      YearBuilt <= 1978, "1944-1978", ifelse(
        YearBuild <= 2010, "1978-2010", ">2010"
  )))))


ggplot(data = house, 
       mapping = aes(x = GrLivArea,y = SalePrice, color = age_category))  
  geom_point(size=2, shape = 16)  
  scale_color_manual(values = c(
    "1872-1908" = "purple", "1909-1943" = "orange", 
    "1944-1978" = "green", "1978-2010" = "red",
    ">2010" = "white"
  )  
  labs(color = "Year Built")
  • Related