0

This line of code is not working. The problem is when I add this geom_text line that is meant to filter out certain points of the data and label them.

This is the code:

ggplot(data=bg_total_df, aes(x = bg_total_num, y = bg_total$bloodGlucose)) +
  geom_text(data = subset(bg_total, bloodGlucose < 78), label = bg_total$bloodGlucose)+
geom_point(color = ifelse(bg_total$bloodGlucose < 78, "red", "blue"))

Error: Aesthetics must be either length 1 or the same as the data (16): x, y

The ggplot + geom_point code work together just fine. It's when the geom_text code is added where I get this error.

I have been trying to fix this geom_text set of code so that the outliers of the graph (bloodGlucose is less than 78 and greater than 271) will be red and labeled with their number amount. The code that I have written is my first attempt at doing this by starting with the values less than 78.

Here is the code to set up the df.

bg_total <- filter(csutrauma.data, bloodGlucose >= 0)
bg_total_num <- 1:328

bg_total_df <- data.frame(bg_total_num, bg_total$bloodGlucose)

bg_total$bloodGlucose (these are the filtered out blood glucose data) <-  c(118.0,  72.0, 207.0, 107.0,  99.0, 106.0,  98.0,  89.0, 114.0,  98.0, 109.0, 114.0,  85.0, 138.0, 100.0, 118.0,  99.0, 104.0,  80.0, 105.0, 113.0,  89.0, 116.0, 121.0, 120.0, 109.0,  97.0, 100.0, 112.0, 133.0,  95.0, 107.0,  91.0, 133.0,  80.0, 114.0, 118.0, 109.0, 111.0, 115.0, 122.0,  90.0, 114.0,  93.0, 106.0, 555.0, 106.0, 106.0, 119.0,  95.0, 153.0, 118.0,  75.0, 112.0, 102.0, 103.0, 150.0, 109.0, 110.0, 206.0, 103.0, 266.0, 103.0, 104.0, 119.0, 106.0,  94.0, 94.0, 119.0, 120.0, 96.0, 252.0, 121.0, 126.0, 317.0, 170.0, 119.0,  98.0, 124.0, 137.0, 255.0, 136.0, 126.0, 0.0, 117.0, 134.0, 133.0, 102.0, 150.0, 232.0, 132.0, 112.0, 78.0, 155.0, 113.0, 122.0, 153.0,  96.0,  83.0, 154.0,  41.0,  91.0, 120.0, 134.0, 126.0, 168.0, 196.0, 125.0, 105.0, 105.0,  93.0, 88.0,  89.0,  93.0, 120.0,  93.0,  93.0, 135.0, 102.0,  86.0,  93.0, 141.0, 150.0,  96.0, 101.0,  61.0, 110.0,  81.0,  97.0, 140.0,  95.0, 106.0, 117.0, 119.0, 139.0, 110.0, 101.0, 128.0, 107.0, 160.0, 122.0, 135.0,  59.0, 102.0,  72.0, 145.0, 104.0, 134.0, 113.0, 105.0, 107.0, 100.0,  65.0, 100.0, 136.0, 110.0, 159.0, 107.0, 115.0, 101.0, 493.0,  78.0,  84.0, 229.0, 102.0, 172.0, 139.0, 102.0, 541.0,  94.0,  91.0,  88.0, 181.0, 129.0, 123.0, 108.0, 115.0,  97.0, 159.0, 124.0, 118.0, 138.0, 182.0, 115.0, 119.0, 101.0,  99.0,  88.0, 134.0, 104.0, 152.0, 130.0,  90.0, 100.0, 157.0, 112.0, 120.0, 110.0, 112.0, 104.0,  71.0, 120.0, 254.0, 118.0, 119.0, 94.0, 104.0, 183.0, 227.0, 210.0, 150.0,  93.0,  91.0,  99.0,  86.0,  90.0, 115.0, 247.0, 111.0, 109.0, 90.0, 113.0, 98.0, 80.0, 145.0, 101.0,  94.0,  94.0, 138.0, 241.0,   3.8, 106.0,  93.0, 96.0, 105.0, 181.0,  95.0,  82.0,  82.0,  18.0, 148.0, 175.0, 323.0, 118.0,  89.0, 100.0, 117.0, 175.0, 200.0, 147.0, 108.0, 210.0, 123.0, 136.0, 107.0, 117.0, 116.0, 154.0,  56.0, 102.0, 105.0, 100.0, 108.0, 226.0, 149.0, 86.0, 116.0, 140.0, 133.0, 121.0, 122.0,  98.0,  95.0, 118.0, 114.0, 113.0, 94.0,  70.0,  52.0, 110.0, 189.0,  82.0 200.0,  89.0,  95.0,  93.0, 111.0, 117.0, 110.0, 156.0,  87.0, 136.0, 336.0, 112.0, 128.0,  92.0, 61.0, 130.0, 119.0, 152.0,  90.0, 111.0, 116.0, 133.0,  94.0, 326.0, 121.0, 119.0 121.0 152.0, 113.0, 128.0, 123.0, 155.0, 176.0, 101.0, 248.0,  64.0, 90.0, 105.0, 235.0, 124.0, 142.0, 388.0, 118.0, 202.0, 156.0, 195.0)

Thank you in advance.

tjebo
  • 21,977
  • 7
  • 58
  • 94
Jack Thomas
  • 37
  • 1
  • 5
  • 1
    You are trying to map aesthetics to data but you are not putting it inside an `aes` call. Try something like `geom_text(aes(label = bloodGlucose), data = subset(bg_total, bloodGlucose < 78))` – Jack Brookes Jul 06 '18 at 23:08
  • 3
    And never use `data$column` inside `aes()`. Just use the column name. [See this answer for more info](https://stackoverflow.com/a/51194689/903061) – Gregor Thomas Jul 07 '18 at 01:45
  • I tried the code you have mentioned - it is still printing out the same error. The bg_total$bloodGlucose column has 328 rows. Whereas the subset has 16 points - I am not sure if it has to do with this, but I've seen other examples where the subsetting works in their case. I have been trying all kinds of edits, but with the same error every time. – Jack Thomas Jul 07 '18 at 18:25
  • Maybe you can add to your post the edits you've made that other commenters suggested. More crucially, you haven't [posted any data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), which makes it difficult for folks to provide much more than guesses as to what's going on – camille Jul 07 '18 at 18:53
  • Sorry about that. I just updated the post to show the code to set up the df, as well as the data I'm using. – Jack Thomas Jul 07 '18 at 19:36
  • your code to produce your dataframe is not reproducible like that - please rearrange. It is also good practice to try one's code in an *empty* R session. Also, reduce your problem to the minimum, and thus your code. You very likely do not need 328 rows to reproduce your problem. – tjebo Jul 07 '18 at 21:32
  • It's 328 rows out of an excel sheet with 80 columns. I just wrote it this way because I don't know how to make a dataframe for ggplot without using 1:328 as the rows. Please let me know how I can fix this if possible. – Jack Thomas Jul 07 '18 at 22:38
  • I suggest to read the top answer at this question [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – tjebo Jul 08 '18 at 04:54
  • Have you worked with `ggplot` before? I think you would benefit from just working your way through some tutorials and then revisiting this. [R Graphics Cookbook](http://www.cookbook-r.com/Graphs/) is a great place to start – camille Jul 08 '18 at 13:22

0 Answers0