-2

I am making a plot to show the relationship between house size and prices. The thing is, I need the 5% of the most recently built houses to have a different color and symbol on the plot. Here is my code (new_baltimore is the dataframe):

    y <- new_baltimore$AGE
    quantile(y, 0.05)   #the result is 4
    k <- subset(new_baltimore, y<=4)
    kk <- k$SQFT
    col = ifelse(any new_baltimore$SQFT %in% kk, "red", "green")
    pch = ifelse(any new_baltimore$SQFT %in% kk, 25, 20)
    plot(new_baltimore$SQFT, PRICE, col=col, pch=pch)

R gives me the errors

Error: unexpected symbol in "col = ifelse(any new_baltimore"
Error: unexpected symbol in "pch = ifelse(any new_baltimore"

Any help?

edit: This is the reproducible data:

baltimore_struct <-
  structure(
    list(
      new_baltimore.SQFT = c(
        11.25,
        28.92,
        30.62,
        26.12,
        22.04,
        39.42,
        21.88,
        25.6,
        44.12,
        19.88,
        12.08,
        10.99,
        12.8,
        29.79,
        14.3,
        13.72,
        11.84,
        18.06,
        10.72,
        8.96,
        14.38,
        36.75,
        20,
        22.82,
        24.86,
        19.2,
        11.58,
        26,
        14.4,
        11.62,
        23.08,
        23.76,
        15.6,
        10,
        22.8,
        16.76,
        22.1,
        14.28,
        15.36,
        16,
        23.04,
        24.94,
        11.82,
        12.88,
        11.2,
        18.12,
        38.25,
        17.68,
        19.02,
        32.8,
        15.16,
        21.975,
        12.6,
        23.52,
        17.52,
        47.61,
        20.55,
        35.52,
        8.4,
        13.68,
        14.48,
        12.8,
        12.8,
        18,
        15.4,
        10.08,
        8.96,
        8.96,
        20,
        12.88,
        12,
        18.16,
        14.28,
        26,
        12.02,
        20.8,
        11.78,
        8.68,
        17.6,
        11.4,
        44.55,
        46.32,
        10.24,
        9.6,
        31.2,
        26.4,
        13.6,
        27.48,
        17.86,
        18.04,
        14.84,
        10.46,
        14.56,
        6.96,
        9.5,
        11.86,
        12.88,
        12.32,
        6.72,
        10.08,
        15.6,
        6.72,
        11.52,
        11.76,
        10.24,
        11.52,
        9.28,
        6.72,
        15.6,
        15.5,
        9.84,
        15.6,
        13.76,
        10.24,
        5.76,
        10.08,
        11.52,
        12.15,
        9.77,
        15,
        14.4,
        14.5,
        22.54,
        10.24,
        7.8,
        8.4,
        10.92,
        42.9,
        9,
        10.5,
        10.08,
        12.6,
        8.96,
        8.58,
        7.56,
        10.8,
        13.44,
        10.24,
        14.44,
        12.24,
        13.2,
        9.6,
        15.22,
        24.16,
        10.24,
        10.24,
        9.88,
        23.2,
        17.68,
        24.3,
        35.94,
        21.6,
        11.02,
        21,
        23.92,
        14.4,
        28,
        11.44,
        21.94,
        10.24,
        16.86,
        9.92,
        13.44,
        12,
        14.76,
        8.96,
        11.52,
        8.64,
        8.12,
        11.12,
        11.28,
        10.36,
        11.52,
        17.1,
        17.52,
        10.73,
        11.2,
        12.8,
        12,
        41.07,
        12.8,
        22.36,
        10.56,
        13.44,
        11.02,
        17.98,
        18.88,
        11.76,
        9.36,
        11.52,
        27.3,
        23.04,
        17.68,
        13.36,
        11.6,
        11.52,
        9.98,
        12.96,
        11.13,
        19.6,
        11.52,
        12.16,
        0,
        10.64
      ),
      PRICE2 = c(
        47,
        113,
        165,
        104.3,
        62.5,
        70,
        127.5,
        64.5,
        145,
        63.5,
        58.9,
        65,
        48,
        3.5,
        12.8,
        17.5,
        36,
        41.9,
        53.5,
        24.5,
        24.5,
        55.5,
        60,
        51,
        46,
        46,
        44,
        54.9,
        42.5,
        44,
        44.9,
        37.9,
        33,
        43.9,
        49.6,
        52,
        37.5,
        50,
        35.9,
        42.9,
        107,
        112,
        44.9,
        55,
        102,
        35.5,
        62.9,
        39,
        110,
        8,
        62,
        85.9,
        57,
        110,
        67.7,
        89.5,
        70,
        74,
        13,
        48,
        24,
        53.5,
        34.5,
        53,
        87.5,
        33.5,
        24,
        9.6,
        30,
        41,
        30,
        38.9,
        20.7,
        49.9,
        18.6,
        39,
        34,
        16,
        18.9,
        15.2,
        41.5,
        53,
        22,
        24.9,
        6.7,
        32.5,
        30,
        59,
        29.5,
        26,
        16.5,
        39,
        48.9,
        33.5,
        46,
        54,
        57.9,
        37.9,
        32,
        31,
        34,
        29,
        32.5,
        51.9,
        31,
        41.8,
        48,
        28,
        35,
        46.5,
        51.9,
        35.4,
        16,
        35,
        35,
        36.5,
        35.9,
        45,
        40,
        35,
        38,
        37,
        23,
        25.5,
        39.5,
        21.5,
        9,
        67.5,
        13.4,
        12.5,
        28.5,
        23,
        33.5,
        9,
        11,
        30.9,
        31.65,
        33,
        33.4,
        47,
        40,
        46,
        45.5,
        57,
        29.9,
        30,
        34,
        51,
        64.5,
        57.5,
        85.5,
        61,
        38,
        56.5,
        60.4,
        51.5,
        54,
        69,
        56,
        27.9,
        37.5,
        32.9,
        22,
        29.9,
        39.9,
        32.6,
        38.5,
        21.5,
        25.9,
        27.5,
        22.9,
        31.5,
        8.5,
        5.5,
        33,
        57,
        47,
        43.5,
        43.9,
        68.5,
        44.25,
        61,
        40,
        44.5,
        57,
        35,
        35.1,
        64.5,
        40,
        42.6,
        50,
        58,
        58,
        55,
        43,
        54,
        39,
        45,
        42,
        38.9,
        43.215,
        26.5,
        30,
        29.5
      )
    ),
    row.names = c(
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      29L,
      30L,
      31L,
      32L,
      33L,
      34L,
      35L,
      36L,
      37L,
      38L,
      39L,
      40L,
      41L,
      42L,
      43L,
      44L,
      45L,
      46L,
      47L,
      48L,
      49L,
      50L,
      51L,
      53L,
      54L,
      55L,
      56L,
      57L,
      58L,
      59L,
      60L,
      61L,
      62L,
      63L,
      64L,
      65L,
      66L,
      67L,
      68L,
      69L,
      70L,
      71L,
      72L,
      73L,
      74L,
      75L,
      76L,
      77L,
      78L,
      79L,
      80L,
      81L,
      82L,
      83L,
      84L,
      85L,
      86L,
      87L,
      88L,
      89L,
      90L,
      91L,
      92L,
      93L,
      94L,
      95L,
      96L,
      97L,
      98L,
      99L,
      100L,
      101L,
      102L,
      103L,
      104L,
      105L,
      106L,
      107L,
      108L,
      109L,
      110L,
      111L,
      112L,
      113L,
      114L,
      115L,
      116L,
      117L,
      118L,
      119L,
      120L,
      121L,
      122L,
      123L,
      124L,
      125L,
      126L,
      127L,
      128L,
      129L,
      130L,
      131L,
      132L,
      133L,
      134L,
      135L,
      136L,
      137L,
      138L,
      139L,
      140L,
      141L,
      142L,
      143L,
      144L,
      145L,
      146L,
      147L,
      148L,
      149L,
      150L,
      151L,
      152L,
      153L,
      154L,
      155L,
      156L,
      157L,
      158L,
      159L,
      160L,
      161L,
      162L,
      163L,
      164L,
      165L,
      166L,
      167L,
      168L,
      169L,
      170L,
      171L,
      172L,
      173L,
      174L,
      175L,
      176L,
      177L,
      178L,
      179L,
      180L,
      181L,
      182L,
      183L,
      184L,
      185L,
      186L,
      187L,
      188L,
      189L,
      190L,
      191L,
      192L,
      193L,
      194L,
      195L,
      196L,
      197L,
      198L,
      199L,
      200L,
      201L,
      202L,
      203L,
      204L,
      205L
    ),
    class = "data.frame"
  )

edit2: I found the error, I just had to remove the any in the ifelse commands. So the correct code looks like this

col = ifelse(new_baltimore$SQFT %in% kk, "red", "green")
pch = ifelse(new_baltimore$SQFT %in% kk, 25, 20)
Anna
  • 55
  • 5

1 Answers1

0

next time, please try to provide some reproducible data. It makes it easier for others to help you. You can find more information here.

I tried to generate some data on my own. Hope it fits your data. I recommend using the tidyverse as it contains a lot of useful packages for manipulating and visualising data. library(tidyverse) loads all packages from the tidyverse but you can also load only the necessary packages such as dplyr for data manipulation or ggplot2 for data visualisation. See comments for further details:

#install.packages("tidyverse")
library(tidyverse) 

# generate some data
data <- data.frame(
  age = sample(c(10:50), 100, replace=TRUE),
  price = sample(c(50000:1000000), 100, replace=TRUE),
  size = sample(c(200:500), 100, replace=TRUE)
)

# save threshold
threshold <- quantile(data$age, 0.05) 

plot_data <- data %>%
  mutate(groups = factor(ifelse(age<threshold, "newer", "older"))) # mutate generates a new variable in the data and you can define groups based on conditions

ggplot(plot_data, 
       aes(x=size, y=price, group=groups, color=groups, shape=groups)) + # here you specify the mappings of the aesthetics and group the data depending on the groups variable that corresponds to our threshold
  geom_point(size=4) +
  scale_color_manual(values = c("aquamarine4","burlywood")) + # control color aesthetic
  scale_shape_manual(values = c(18, 19)) + # control shape aesthetic
  theme_classic() # this one of a lot of predefined themes

Example

Hope that helps.

fbeese
  • 118
  • 8
  • I shared the data, sorry it took me a bit of time, never used dput before. Thank you for the answer, I will give it a try now. – Anna Jun 26 '22 at 15:05