Can't find the length mismatch that R errors on

I expect the code below to produce a line chart with black lines and error bars, plus points that have the designated colors, outlined in black. (Points can have both; the others have only color.)

Instead, I get an error:

! Aesthetics must be either length 1 or the same as the data (44)
:heavy_multiplication_x: Fix the following mappings: fill

The scale_fill statement defines 11 colors, ColorN contains 11 numbers, and TC has 11 rows. I don't understand where there's a length mismatch.

ColorN = as.factor(1:11)

ggplot(CRatio, aes(x = CR, y = Mean, fill = ColorN, color = "black")) +
  geom_line(show.legend = FALSE, size = 0.6) +
  geom_errorbar(show.legend = FALSE, ymin = CRatio$Mean - CRatio$FLSD, ymax = CRatio$Mean + CRatio$FLSD, width = 0.12, size = 0.6) +
  geom_point(show.legend = FALSE, size = 2) +
  scale_fill_manual(values = c("1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3", "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8", "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334")) +
  xlab("Contrast Ratio") +
  ylab("Score")

This is CRatio in its entirety:

##           CR N     Mean        SD     FLSD
## 1   2.998333 8 98.51484 2.2059607 1.429252
## 2   3.108333 8 98.69150 2.5807032 1.429252
## 3   3.180000 8 97.19956 3.6635529 1.429252
## 4   3.300000 8 99.62121 1.0713739 1.429252
## 5   3.361667 8 99.07280 1.7184060 1.429252
## 6   3.491667 8 99.03826 2.2359232 1.429252
## 7   3.543333 8 98.46969 1.8257819 1.429252
## 8   3.566667 8 97.87017 4.3723549 1.429252
## 9   3.683333 8 99.40248 1.2322768 1.429252
## 10  3.800000 8 99.08854 2.3357171 1.429252
## 11  4.033333 8 96.34522 6.4676819 1.429252
## 12  4.116667 8 98.07239 4.0054505 1.429252
## 13  4.266667 8 99.84888 0.2799125 1.429252
## 14  4.400000 8 98.82275 2.2450952 1.429252
## 15  4.683333 8 99.08420 2.0983847 1.429252
## 16  4.758333 8 97.54180 3.7142882 1.429252
## 17  4.850000 8 97.51080 2.7521562 1.429252
## 18  4.966667 8 97.98772 1.6890816 1.429252
## 19  5.100000 8 99.08951 2.5752654 1.429252
## 20  5.125000 8 99.16667 2.3570226 1.429252
## 21  5.200000 8 97.94850 3.3604801 1.429252
## 22  5.441667 8 97.57339 4.5016953 1.429252
## 23  5.500000 8 97.91835 3.4528359 1.429252
## 24  5.550000 8 97.10219 4.0407240 1.429252
## 25  5.583333 8 99.00000 2.8284271 1.429252
## 26  5.783333 8 99.12898 1.4625682 1.429252
## 27  5.875000 8 98.20000 2.6629516 1.429252
## 28  5.900000 8 97.71143 3.6613992 1.429252
## 29  6.000000 8 95.74095 6.2436828 1.429252
## 30  6.041667 8 97.60755 6.5195377 1.429252
## 31  6.250000 8 99.06346 2.4092048 1.429252
## 32  6.416667 8 98.13807 5.2663441 1.429252
## 33  6.500000 8 96.74669 6.4315022 1.429252
## 34  6.833333 8 99.10304 1.4920179 1.429252
## 35  6.958333 8 98.87785 1.5580408 1.429252
## 36  7.416667 8 99.02886 1.4782993 1.429252
## 37  8.333333 8 98.48485 4.2854956 1.429252
## 38  9.000000 8 97.90073 3.7484537 1.429252
## 39  9.666667 8 98.40473 1.7253653 1.429252
## 40 10.166667 8 99.47872 1.2501322 1.429252
## 41 10.333333 8 98.89801 1.4817396 1.429252
## 42 11.000000 8 99.03092 1.8017662 1.429252
## 43 11.833333 8 98.10606 5.3568696 1.429252
## 44 12.666667 8 99.16574 1.5458937 1.429252

At the moment I do not see see where "CR" is coming from.

Can you supply the data isndput format and also post the error message please?

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

1 Like

Oops -- I copied the wrong data frame. Thanks for pointing that out. I've corrected my original post.

The error message is quoted there.

Output from dput(head(dataset, 36)):

## structure(list(TestName = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Weather search", 
## "Shape search", "Color+Shape search"), class = "factor"), SubjectID = structure(c(1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L), levels = c("2", "4", "6", "9", "11", "13", "14", 
## "15", "18", "19", "20", "21", "22", "24", "26", "31", "32", "33", 
## "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", 
## "45", "46", "47", "48", "49", "50", "51"), class = "factor"), 
##     Backlight = c(450, 450, 450, 450, 450, 450, 450, 450, 450, 
##     450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 
##     450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 450, 
##     450, 450, 450), TargetColor = c("WxGREEN", "WxGREEN", "WxGREEN", 
##     "WxGREEN", "WxGREEN", "WxGREEN", "WxGREEN", "WxGREEN", "WxGREEN", 
##     "WxGREEN", "WxGREEN", "WxGREEN", "WxGREEN", "WxGREEN", "WxRED", 
##     "WxRED", "WxRED", "WxRED", "WxRED", "WxRED", "WxRED", "WxRED", 
##     "WxRED", "WxRED", "WxRED", "WxRED", "WxRED", "WxRED", "WxYELLOW", 
##     "WxYELLOW", "WxYELLOW", "WxYELLOW", "WxYELLOW", "WxYELLOW", 
##     "WxYELLOW", "WxYELLOW"), Participant = c(2, 2, 2, 2, 2, 2, 
##     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
##     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), NumTrials = c(1, 1, 1, 
##     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
##     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), NumTargets = c(100, 
##     100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
##     100, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 
##     94, 100, 100, 100, 100, 100, 100, 100, 100), NumDistractors = c(572, 
##     572, 572, 572, 572, 572, 572, 572, 572, 572, 572, 572, 572, 
##     572, 578, 578, 578, 578, 578, 578, 578, 578, 578, 578, 578, 
##     578, 578, 578, 572, 572, 572, 572, 572, 572, 572, 572), NumHits = c(0, 
##     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 84, 84, 84, 84, 
##     84, 84, 84, 84, 84, 84, 84, 84, 84, 97, 97, 97, 97, 97, 97, 
##     97, 97), NumMisses = c(100, 100, 100, 100, 100, 100, 100, 
##     100, 100, 100, 100, 100, 100, 100, 10, 10, 10, 10, 10, 10, 
##     10, 10, 10, 10, 10, 10, 10, 10, 3, 3, 3, 3, 3, 3, 3, 3), 
##     NumFAs = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 
##     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 
##     0), Score = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88.4966502245454, 
##     88.4966502245454, 88.4966502245454, 88.4966502245454, 88.4966502245454, 
##     88.4966502245454, 88.4966502245454, 88.4966502245454, 88.4966502245454, 
##     88.4966502245454, 88.4966502245454, 88.4966502245454, 88.4966502245454, 
##     88.4966502245454, 97, 97, 97, 97, 97, 97, 97, 97), AvgResponseSpeed = c(0, 
##     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.730981234227213, 
##     0.730981234227213, 0.730981234227213, 0.730981234227213, 
##     0.730981234227213, 0.730981234227213, 0.730981234227213, 
##     0.730981234227213, 0.730981234227213, 0.730981234227213, 
##     0.730981234227213, 0.730981234227213, 0.730981234227213, 
##     0.730981234227213, 1.19592856864349, 1.19592856864349, 1.19592856864349, 
##     1.19592856864349, 1.19592856864349, 1.19592856864349, 1.19592856864349, 
##     1.19592856864349), ColorVision = structure(c(1L, 1L, 1L, 
##     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
##     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
##     1L, 1L, 1L), levels = c("NCV", "CVD"), class = "factor"), 
##     TDW.ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
##     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
##     1), CR = c(1.24, 1.24, 1.24, 1.24, 1.24, 1.24, 1.24, 1.24, 
##     1.24, 1.24, 1.24, 1.24, 1.24, 1.24, 1.375, 1.375, 1.375, 
##     1.375, 1.375, 1.375, 1.375, 1.375, 1.375, 1.375, 1.375, 1.375, 
##     1.375, 1.375, 1.5325, 1.5325, 1.5325, 1.5325, 1.5325, 1.5325, 
##     1.5325, 1.5325)), class = c("grouped_df", "tbl_df", "tbl", 
## "data.frame"), row.names = c(NA, -36L), groups = structure(list(
##     TestName = structure(c(1L, 1L, 1L), levels = c("Weather search", 
##     "Shape search", "Color+Shape search"), class = "factor"), 
##     SubjectID = structure(c(1L, 1L, 1L), levels = c("2", "4", 
##     "6", "9", "11", "13", "14", "15", "18", "19", "20", "21", 
##     "22", "24", "26", "31", "32", "33", "34", "35", "36", "37", 
##     "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", 
##     "48", "49", "50", "51"), class = "factor"), Backlight = c(450, 
##     450, 450), TargetColor = c("WxGREEN", "WxRED", "WxYELLOW"
##     ), .rows = structure(list(1:14, 15:28, 29:36), ptype = integer(0), class = c("vctrs_list_of", 
##     "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
## ), row.names = c(NA, -3L), .drop = TRUE))

So it has more than 11 entries....
aes() is about mapping a variable in your data frame to a chart aesthetic, there are presumably 44 rows in your frame and 11 entries in the variable you try to use as fill. This cant work. If you want a fill aesthetic it should be from a variable in your frame.

1 Like

Ah, yes -- I see. Thank you. I changed the first line to:

ColorN = as.factor(c(1:11, 1:11, 1:11, 1:11))

But now I get :

! Aesthetics must be either length 1 or the same as the data (11)
✖ Fix the following mappings: `fill`

*Ah, yes -- I see. Thank you. I changed the first line to:

ColorN = as.factor(c(1:11, 1:11, 1:11, 1:11))*

Why?

We really need to see what your data looks like. See reference to dput() above.

1 Like

Why? Because-- as nirgrahamuk noted -- the data frame has 4 x 11 = 44 rows.

I posted the dput output earlier, but it's been pulled for review because the Posit bot thinks I'm one. (Maybe it wants a date. :heart_eyes: )

Here it is again:

## structure(list(TestName = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Weather search", 
## "Shape search", "Color+Shape search"), class = "factor"), SubjectID = structure(c(1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 11L, 11L, 11L, 11L, 
## 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
## 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
## 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
## 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L
## ), levels = c("2", "6", "9", "11", "13", "14", "15", "18", "20", 
## "21", "22", "24", "25", "26", "31", "32", "33", "34", "35", "36", 
## "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", 
## "48", "49"), class = "factor"), Backlight = c(550, 550, 550, 
## 550, 550, 550, 550, 550, 550, 550, 550, 600, 600, 600, 600, 600, 
## 600, 600, 600, 600, 600, 600, 650, 650, 650, 650, 650, 650, 650, 
## 650, 650, 650, 650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 
## 700, 700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 
## 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 650, 650, 
## 650, 650, 650, 650, 650, 650, 650, 650, 650, 700, 700, 700, 700, 
## 700, 700, 700, 700, 700, 700, 700, 550, 550, 550, 550, 550, 550, 
## 550, 550, 550, 550, 550, 600), TargetColor = c("AQUA", "BLUE", 
## "BROWN", "GRAY", "GREEN", "MAGENTA", "ORANGE", "PINK", "RED", 
## "WHITE", "YELLOW", "AQUA", "BLUE", "BROWN", "GRAY", "GREEN", 
## "MAGENTA", "ORANGE", "PINK", "RED", "WHITE", "YELLOW", "AQUA", 
## "BLUE", "BROWN", "GRAY", "GREEN", "MAGENTA", "ORANGE", "PINK", 
## "RED", "WHITE", "YELLOW", "AQUA", "BLUE", "BROWN", "GRAY", "GREEN", 
## "MAGENTA", "ORANGE", "PINK", "RED", "WHITE", "YELLOW", "AQUA", 
## "BLUE", "BROWN", "GRAY", "GREEN", "MAGENTA", "ORANGE", "PINK", 
## "RED", "WHITE", "YELLOW", "AQUA", "BLUE", "BROWN", "GRAY", "GREEN", 
## "MAGENTA", "ORANGE", "PINK", "RED", "WHITE", "YELLOW", "AQUA", 
## "BLUE", "BROWN", "GRAY", "GREEN", "MAGENTA", "ORANGE", "PINK", 
## "RED", "WHITE", "YELLOW", "AQUA", "BLUE", "BROWN", "GRAY", "GREEN", 
## "MAGENTA", "ORANGE", "PINK", "RED", "WHITE", "YELLOW", "AQUA", 
## "BLUE", "BROWN", "GRAY", "GREEN", "MAGENTA", "ORANGE", "PINK", 
## "RED", "WHITE", "YELLOW", "AQUA"), NumTrials = c(4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
## 4L), NumTargets = c(32, 29, 28, 28, 30, 31, 27, 31, 27, 33, 34, 
## 28, 30, 26, 30, 29, 27, 30, 28, 25, 25, 26, 35, 31, 34, 33, 30, 
## 30, 34, 31, 28, 25, 27, 31, 29, 37, 29, 29, 28, 26, 26, 32, 28, 
## 30, 30, 30, 28, 36, 33, 29, 32, 31, 34, 26, 32, 31, 29, 36, 28, 
## 28, 26, 31, 30, 29, 28, 31, 31, 29, 22, 35, 35, 29, 26, 36, 27, 
## 29, 30, 32, 32, 25, 27, 37, 29, 27, 30, 23, 25, 32, 31, 30, 28, 
## 21, 33, 29, 25, 30, 22, 28, 33, 35), NumDistractors = c(160, 
## 163, 164, 164, 162, 161, 165, 161, 165, 159, 158, 164, 162, 166, 
## 162, 163, 165, 162, 164, 167, 167, 166, 157, 161, 158, 159, 162, 
## 162, 158, 161, 164, 167, 165, 161, 163, 155, 163, 163, 164, 166, 
## 166, 160, 164, 162, 162, 162, 164, 156, 159, 163, 160, 161, 158, 
## 166, 160, 161, 163, 156, 164, 164, 166, 161, 162, 163, 164, 161, 
## 161, 163, 170, 157, 157, 163, 166, 156, 165, 163, 162, 160, 160, 
## 167, 165, 155, 163, 165, 162, 169, 167, 160, 161, 162, 164, 171, 
## 159, 163, 167, 162, 170, 164, 159, 157), NumHits = c(32, 29, 
## 28, 28, 30, 31, 26, 31, 27, 33, 34, 28, 30, 26, 30, 28, 27, 29, 
## 28, 25, 25, 26, 35, 31, 34, 32, 29, 30, 31, 31, 28, 25, 27, 31, 
## 29, 36, 29, 29, 28, 26, 26, 32, 28, 30, 30, 30, 28, 36, 33, 29, 
## 32, 31, 33, 26, 32, 30, 29, 36, 28, 28, 26, 31, 30, 29, 28, 31, 
## 31, 29, 22, 35, 35, 29, 26, 36, 27, 29, 29, 32, 32, 25, 27, 37, 
## 29, 27, 30, 23, 25, 32, 31, 30, 28, 21, 33, 29, 25, 30, 22, 27, 
## 33, 35), NumMisses = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
## 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 3, 0, 0, 0, 0, 0, 
## 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
## 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
## 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
## 1, 0, 0), NumFAs = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
## 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
## 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
## 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
## 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
## 0, 0), Score = c(100, 100, 100, 100, 100, 100, 95.6902356902357, 
## 100, 100, 100, 100, 100, 100, 100, 100, 96.551724137931, 100, 
## 96.6666666666667, 100, 100, 100, 100, 100, 100, 99.3670886075949, 
## 96.969696969697, 96.6666666666667, 99.3827160493827, 91.1764705882353, 
## 100, 100, 100, 100, 100, 100, 97.2972972972973, 100, 100, 100, 
## 99.3975903614458, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
## 100, 100, 100, 97.0588235294118, 100, 100, 96.7741935483871, 
## 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
## 100, 100, 100, 100, 100, 100, 100, 96.6666666666667, 100, 100, 
## 100, 100, 100, 100, 100, 100, 100, 100, 99.375, 100, 100, 100, 
## 100, 100, 100, 100, 100, 100, 96.4285714285714, 100, 100), AvgResponseSpeed = c(0.815092608753935, 
## 0.59603489935934, 0.643749157711103, 0.76358511699706, 0.917352983336634, 
## 0.733750253630937, 0.600579804926536, 0.745603452079756, 0.651199640230713, 
## 0.879359292140358, 0.875153232960057, 0.843077048201385, 0.704314715041081, 
## 0.737241828878482, 0.731816510801451, 0.635867566052042, 0.61369714707977, 
## 0.750191219453807, 0.80732585255783, 0.599623717333961, 0.680419162397026, 
## 0.709283693330646, 0.791740848194264, 0.785658469231027, 0.918534996230426, 
## 0.832597693992191, 0.76853827406376, 0.728833012986197, 0.83346447984805, 
## 0.731328646733467, 0.746005285670426, 0.753088161302257, 0.696616581578373, 
## 0.768028552189906, 0.699040117777241, 0.947842330808812, 0.744500863726335, 
## 0.737641371246149, 0.797841991146446, 0.806682982884611, 0.775138661589523, 
## 0.812746902234175, 0.729619355449169, 0.810325788724482, 0.654730471797511, 
## 0.547836533743757, 0.678801714417074, 0.821598246282207, 0.695524491713822, 
## 0.650737195074008, 0.681204598979969, 0.654802674558012, 0.586474909558924, 
## 0.67673690361993, 0.73112731858745, 0.674049551045382, 0.555733297462689, 
## 0.634696744010402, 0.693563727473845, 0.68949494239476, 0.639436592302587, 
## 0.529480367876834, 0.744553762640442, 0.588528313956948, 0.622206426049749, 
## 0.747172570201954, 0.658714913618239, 0.547080383805464, 0.542367043202062, 
## 0.77304095340635, 0.84565458253639, 0.688327025755799, 0.569065255414867, 
## 0.628583395682208, 0.673435324067371, 0.764798470795317, 0.521195628739949, 
## 0.716964483033266, 0.6146317561746, 0.627600557285086, 0.623771371130809, 
## 0.705250371308784, 0.605606989000578, 0.55437494423452, 0.741193317877912, 
## 0.502051440193276, 0.577967500025718, 0.800397652087261, 0.842137021975349, 
## 0.866754252347892, 0.774953671410299, 0.701398155170747, 0.824532786824341, 
## 0.671334258372891, 0.779813604675182, 0.764082412740016, 0.613993597530019, 
## 0.793348080006643, 0.806597958185982, 0.948988406774417), ColorVision = structure(c(1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
## 2L, 2L, 2L), levels = c("NCV", "CVD"), class = "factor"), TDW.ID = c(1, 
## 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
## 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
## 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
## 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
## 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), BacklightVal = c(550, 
## 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 600, 600, 600, 
## 600, 600, 600, 600, 600, 600, 600, 600, 650, 650, 650, 650, 650, 
## 650, 650, 650, 650, 650, 650, 700, 700, 700, 700, 700, 700, 700, 
## 700, 700, 700, 700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 
## 550, 550, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 
## 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 700, 700, 
## 700, 700, 700, 700, 700, 700, 700, 700, 700, 550, 550, 550, 550, 
## 550, 550, 550, 550, 550, 550, 550, 600), CR = c(5.58333333333333, 
## 3.56666666666667, 4.11666666666667, 5.125, 6.04166666666667, 
## 3.10833333333333, 4.85, 4.75833333333333, 2.99833333333333, 10.1666666666667, 
## 8.33333333333333, 6, 3.8, 4.4, 5.5, 6.5, 3.3, 5.2, 5.1, 3.18, 
## 11, 9, 6.41666666666667, 4.03333333333333, 4.68333333333333, 
## 5.875, 6.95833333333333, 3.49166666666667, 5.55, 5.44166666666667, 
## 3.36166666666667, 11.8333333333333, 9.66666666666667, 6.83333333333333, 
## 4.26666666666667, 4.96666666666667, 6.25, 7.41666666666667, 3.68333333333333, 
## 5.9, 5.78333333333333, 3.54333333333333, 12.6666666666667, 10.3333333333333, 
## 5.58333333333333, 3.56666666666667, 4.11666666666667, 5.125, 
## 6.04166666666667, 3.10833333333333, 4.85, 4.75833333333333, 2.99833333333333, 
## 10.1666666666667, 8.33333333333333, 6, 3.8, 4.4, 5.5, 6.5, 3.3, 
## 5.2, 5.1, 3.18, 11, 9, 6.41666666666667, 4.03333333333333, 4.68333333333333, 
## 5.875, 6.95833333333333, 3.49166666666667, 5.55, 5.44166666666667, 
## 3.36166666666667, 11.8333333333333, 9.66666666666667, 6.83333333333333, 
## 4.26666666666667, 4.96666666666667, 6.25, 7.41666666666667, 3.68333333333333, 
## 5.9, 5.78333333333333, 3.54333333333333, 12.6666666666667, 10.3333333333333, 
## 5.58333333333333, 3.56666666666667, 4.11666666666667, 5.125, 
## 6.04166666666667, 3.10833333333333, 4.85, 4.75833333333333, 2.99833333333333, 
## 10.1666666666667, 8.33333333333333, 6)), class = c("grouped_df", 
## "tbl_df", "tbl", "data.frame"), row.names = c(NA, -100L), groups = structure(list(
##     TestName = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
##     2L), levels = c("Weather search", "Shape search", "Color+Shape search"
##     ), class = "factor"), SubjectID = structure(c(1L, 1L, 1L, 
##     1L, 11L, 11L, 11L, 11L, 12L, 12L), levels = c("2", "6", "9", 
##     "11", "13", "14", "15", "18", "20", "21", "22", "24", "25", 
##     "26", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
##     "40", "41", "42", "43", "44", "45", "46", "47", "48", "49"
##     ), class = "factor"), Backlight = c(550, 600, 650, 700, 550, 
##     600, 650, 700, 550, 600), .rows = structure(list(1:11, 12:22, 
##         23:33, 34:44, 45:55, 56:66, 67:77, 78:88, 89:99, 100L), ptype = integer(0), class = c("vctrs_list_of", 
##     "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
## ), row.names = c(NA, -10L), .drop = TRUE))

A reprex for CRatio with 44 rows. As far as I know, points (except shapes 21 to 25), lines and error bars have color but no fill. I also put ymin and ymax in an aesthetic. To avoid a warning I changed size to linewidth for lines and error bars.

library(tidyverse)

CRatio <- structure(list(CR = c(2.998333, 3.108333, 3.18, 3.3, 3.361667, 
3.491667, 3.543333, 3.566667, 3.683333, 3.8, 4.033333, 4.116667, 
4.266667, 4.4, 4.683333, 4.758333, 4.85, 4.966667, 5.1, 5.125, 
5.2, 5.441667, 5.5, 5.55, 5.583333, 5.783333, 5.875, 5.9, 6, 
6.041667, 6.25, 6.416667, 6.5, 6.833333, 6.958333, 7.416667, 
8.333333, 9, 9.666667, 10.166667, 10.333333, 11, 11.833333, 12.666667
), N = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), 
    Mean = c(98.51484, 98.6915, 97.19956, 99.62121, 99.0728, 
    99.03826, 98.46969, 97.87017, 99.40248, 99.08854, 96.34522, 
    98.07239, 99.84888, 98.82275, 99.0842, 97.5418, 97.5108, 
    97.98772, 99.08951, 99.16667, 97.9485, 97.57339, 97.91835, 
    97.10219, 99, 99.12898, 98.2, 97.71143, 95.74095, 97.60755, 
    99.06346, 98.13807, 96.74669, 99.10304, 98.87785, 99.02886, 
    98.48485, 97.90073, 98.40473, 99.47872, 98.89801, 99.03092, 
    98.10606, 99.16574), SD = c(2.2059607, 2.5807032, 3.6635529, 
    1.0713739, 1.718406, 2.2359232, 1.8257819, 4.3723549, 1.2322768, 
    2.3357171, 6.4676819, 4.0054505, 0.2799125, 2.2450952, 2.0983847, 
    3.7142882, 2.7521562, 1.6890816, 2.5752654, 2.3570226, 3.3604801, 
    4.5016953, 3.4528359, 4.040724, 2.8284271, 1.4625682, 2.6629516, 
    3.6613992, 6.2436828, 6.5195377, 2.4092048, 5.2663441, 6.4315022, 
    1.4920179, 1.5580408, 1.4782993, 4.2854956, 3.7484537, 1.7253653, 
    1.2501322, 1.4817396, 1.8017662, 5.3568696, 1.5458937), FLSD = c(1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 
    1.429252)), class = "data.frame", row.names = c(NA, -44L))

ColorN = as.factor(c(1:11, 1:11, 1:11, 1:11))

ggplot(CRatio, aes(x = CR, y = Mean, color = ColorN)) +
  geom_line(show.legend = FALSE, linewidth = 0.6) +
  geom_errorbar(show.legend = FALSE, 
                aes(ymin = Mean - FLSD, ymax = Mean + FLSD), 
                width = 0.12, 
                linewidth = 0.6) +
  geom_point(show.legend = FALSE, size = 2) +
  scale_fill_manual(values = c("1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3", 
                               "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8", 
                               "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334")) +
  xlab("Contrast Ratio") +
  ylab("Score")

Created on 2023-06-30 with reprex v2.0.2

1 Like

Progress!

ColorN = as.factor(c(1:11, 1:11,1:11,1:11))

ggplot(CRatio, aes(x = CR, y = Mean, group = CR)) +
  geom_line(show.legend = FALSE, size = 2) +
  geom_errorbar(show.legend = FALSE, aes(ymin = Mean - FLSD, ymax = Mean + FLSD), width = 0.12, size = 0.6) +
  geom_point(show.legend = FALSE, shape = 21, fill = ColorN, color = "black", size = 5) +
  scale_fill_manual(values = c("1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3", "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8", "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334")) +
  xlab("Contrast Ratio") +
  ylab("Score")

Adding "group = CR" and "shape = 21" were critical -- thanks!

Remaining issues:

  1. Don't know why the lines don't appear.
  2. The colors aren't the ones specified in the scale_fill statement.

I need a bit of sleep before the Tour de France starts early tomorrow, but I tried a couple of quick changes. They may or may not make sense:

ggplot(CRatio, aes(x = CR, y = Mean, group = ColorN)) +
  geom_line(show.legend = FALSE, linewidth = 2) +
  geom_errorbar(show.legend = FALSE, aes(ymin = Mean - FLSD, ymax = Mean + FLSD), width = 0.12, size = 0.6) +
  geom_point(show.legend = FALSE, shape = 21, aes(fill = ColorN), color = "black", size = 5) +
  scale_fill_manual(values = c("1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3", 
                               "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8", 
                               "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334")) +
  xlab("Contrast Ratio") +
  ylab("Score")
1 Like

The fill argument is used to further distinguish by discrete variables. You would have to

CRatio$ColorN  <- as.factor(1:11)
1 Like

That code produces a mess:

But that's because I need to group by CR -- not ColorN:

ColorN = as.factor(c(1:11, 1:11,1:11,1:11))

ggplot(CRatio, aes(x = CR, y = Mean, fill = ColorN,  group = CR)) +
  geom_line(show.legend = FALSE, linewidth = 2) +
  geom_errorbar(show.legend = FALSE, aes(ymin = Mean - FLSD, ymax = Mean + FLSD), width = 0.12, size = 0.6) +
  geom_point(show.legend = FALSE, shape = 21, color = "black", size = 5) +
  scale_fill_manual(values = c("1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3", 
                               "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8", 
                               "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334")) +
  xlab("Contrast Ratio") +
  ylab("Score")

The critical change is moving "fill =" into an aes -- doesn't matter whether it's paired with ggplot or geom_point. Recall, though, that I started with fill= and color= inside the first aes -- that didn't work correctly.

So I expect the lines will connect as they should when I get them to appear. And the colors are correct! (That's due to finally getting the group = statement right.)

But where are the black lines?

@technocrat: I think you overlooked the 1st line of my code:

ColorN = as.factor(c(1:11, 1:11,1:11,1:11))

I was working from the OP, which was good, but having to bring in the data separately slowed me down. Please use a complete reprex (see the FAQ).

I saw that the combination of points, error bars and line aesthetics was going to be problematic, but thought to let that question bubble.

I see that you've fixed the first issue, that the error bars extended beyond the range of the y-axis. The second issue, the mess with the line aes raises the question of what useful information the slope of change of Score from Contrast Ratio to Contrast Ratio provides. If it is something to draw attention to, a geom_smooth() aes with or without captures the movement without creating a lot of discontinuities. Finally, you might consider a log scale o the x-axis to allow the relatively greater number of observations below a contrast of 7.5 to have more breathing room.

1 Like

The OP contains the complete data frame. What more is needed for a reprex?

I appreciate your suggested refinements, but they're moot until I get the lines to appear.

A reprex is cut and paste. The data in the OP isn't. It had to be edited to remove the comment hashtags, the row numbering, and to replace the whitespace with commas. A post without an reprex that executes as far as the point at which help is needed is far more likely to receive help.

Lines won't appear until you lose group=CR but then it will be a hot mess.

My ideas for an x-axis log scale didn't sufficiently open up the congestion below 7.5, so I've thinned out the error bars instead since most of the ink is useful only to associate the point estimate with the upper and lower bound, so it doesn't have to be visually prominent. With the grouping, my idea for geom_smooth() no longer makes sense because each would have its own trace.

library(ggplot2)

CRatio <- structure(list(
  CR = c(
    2.998333, 3.108333, 3.18, 3.3, 3.361667,
    3.491667, 3.543333, 3.566667, 3.683333, 3.8, 4.033333, 4.116667,
    4.266667, 4.4, 4.683333, 4.758333, 4.85, 4.966667, 5.1, 5.125,
    5.2, 5.441667, 5.5, 5.55, 5.583333, 5.783333, 5.875, 5.9, 6,
    6.041667, 6.25, 6.416667, 6.5, 6.833333, 6.958333, 7.416667,
    8.333333, 9, 9.666667, 10.166667, 10.333333, 11, 11.833333, 12.666667
  ), N = c(
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L
  ),
  Mean = c(
    98.51484, 98.6915, 97.19956, 99.62121, 99.0728,
    99.03826, 98.46969, 97.87017, 99.40248, 99.08854, 96.34522,
    98.07239, 99.84888, 98.82275, 99.0842, 97.5418, 97.5108,
    97.98772, 99.08951, 99.16667, 97.9485, 97.57339, 97.91835,
    97.10219, 99, 99.12898, 98.2, 97.71143, 95.74095, 97.60755,
    99.06346, 98.13807, 96.74669, 99.10304, 98.87785, 99.02886,
    98.48485, 97.90073, 98.40473, 99.47872, 98.89801, 99.03092,
    98.10606, 99.16574
  ), SD = c(
    2.2059607, 2.5807032, 3.6635529,
    1.0713739, 1.718406, 2.2359232, 1.8257819, 4.3723549, 1.2322768,
    2.3357171, 6.4676819, 4.0054505, 0.2799125, 2.2450952, 2.0983847,
    3.7142882, 2.7521562, 1.6890816, 2.5752654, 2.3570226, 3.3604801,
    4.5016953, 3.4528359, 4.040724, 2.8284271, 1.4625682, 2.6629516,
    3.6613992, 6.2436828, 6.5195377, 2.4092048, 5.2663441, 6.4315022,
    1.4920179, 1.5580408, 1.4782993, 4.2854956, 3.7484537, 1.7253653,
    1.2501322, 1.4817396, 1.8017662, 5.3568696, 1.5458937
  ), FLSD = c(
    1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252, 1.429252, 1.429252, 1.429252, 1.429252, 1.429252,
    1.429252
  )
), class = "data.frame", row.names = c(NA, -44L))

ColorN <- as.factor(c(1:11, 1:11, 1:11, 1:11))

ggplot(CRatio, aes(x = CR, y = Mean, fill = ColorN, group = CR)) +
  geom_errorbar(show.legend = FALSE, aes(ymin = Mean - FLSD, ymax = Mean + FLSD), width = 0.12, linewidth = 0.3) +
  geom_point(show.legend = FALSE, shape = 21, color = "black", size = 5) +
  scale_fill_manual(values = c(
    "1" = "#07CDED", "2" = "#5E8DF6", "3" = "#C5955B", "4" = "#B3B3B3",
    "5" = "#23E162", "6" = "#D822FF", "7" = "#FE930D", "8" = "#F684D8",
    "9" = "#FF1320", "10" = "#FFFFFF", "11" = "#DFF334"
  )) +
  xlab("Contrast Ratio") +
  ylab("Score") +
  theme_minimal()

1 Like

I didn't recognize the editing drudgery my posted data frame would cause. Sorry, and thanks for cluing me in.

Hot mess or not, I'd like to know how to join the points with straight black lines while retaining the figure's other features.

By what principle should one point be joined to another by a line ? Some commonality ? Some particular relationship ? Arbitrary ?

1 Like

The principle is that showing one continuous variable as a function of another is depicted customarily with a continuous line.

I'm reminded of the old joke: "Doctor, it hurts when I do this." I believe the purpose of this forum is to help people get R to do what they want. Perhaps there's a forum for people who want their graphic designs vetted but that's not what I'm seeking.

Im honestly trying to help you but you dont seem to understand how underspecified your requirements are.

We have no context for what your analysis concerns so we cant use background knowledge of your task to infer reasonable approaches for ourselves, hence the questions put to you. Its your perogative to not answer themz but then its a bit much to expect to be helped.
Let me put it like this.
If i identify the point associated with first the first row of your data as shared, which other next point should a line be drawn to ? And why ? If you cannot answer this question i simply cannot help you.

3 Likes

And I honestly appreciate the efforts you and the others are making to help me. I apologize if I've given a different impression.

I'm graphing Score as a function of Contrast Ratio. Therefore each point should have a line connecting it to the point to its immediate left and the point to its immediate right, i.e., the next lowest and highest ratios. The fill colors show the color of the stimulus that was presented at each contrast ratio.