Help with ggplot.

Can someone please help me find what is wrong with this code below:

df <- read.csv("F:/Complete_Projects/acceptability_example.csv", stringsAsFactors = F)
#> Warning in file(file, "rt"): cannot open file 'F:/Complete_Projects/
#> book_chapter_CRC/acceptability_example.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection
colnames(df)[1] = "timedata"
#> Error in `colnames<-`(`*tmp*`, value = "timedata"): attempt to set 'colnames' on an object with less than two dimensions
colnames(df)[2] = "score"
#> Error in `colnames<-`(`*tmp*`, value = c(NA, "score")): attempt to set 'colnames' on an object with less than two dimensions
p <- ggplot(df, aes(x = timedata, y = score)) +
  theme_bw() +
  xlab("Oxidized flavor on 15-point scale") +
  ylab("Overall liking on 9-point hedonic scale") +
  geom_point(color = "#04fbfb", size = 2) +
  geom_smooth(method=lm, linetype = "dashed", color="black") +
  coord_cartesian(ylim=c(1, 9)) + 
  scale_y_continuous(breaks=seq(1, 9, 1)) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label..)), 
               parse = TRUE, label.y = 0.90, label.x = 0.7) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..rr.label..)), 
               parse = TRUE, label.y = 0.85, label.x = 0.7) +
  theme(axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face="bold"),
        axis.text.x= element_text(face = "bold"),
        axis.text.y = element_text(face = "bold"),
        panel.border = element_blank(),
        axis.line = element_line())
#> Error in ggplot(df, aes(x = timedata, y = score)): could not find function "ggplot"
p + annotate("segment", x = 0, xend = 4, y = 5.8, yend = 5.8, colour = "blue", linetype=5) +
  annotate("segment", x = 4, xend = 4, y = 0, yend = 5.8, colour = "blue", linetype=2) +
  annotate("segment", x = 0, xend = 4.9, y = 5.8, yend = 5.8, colour = "blue", linetype=5) +
  annotate("segment", x = 4.9, xend = 4.9, y = 0, yend = 5.8, colour = "red", linetype=2) +
  annotate("segment", x = 0, xend = 4.9, y = 5.8, yend = 5.8, colour = "blue", linetype=5) +
  annotate("segment", x = 3, xend = 3, y = 0, yend = 5.8, colour = "green", linetype=2) +
  annotate("text", x = 1.5, y = 5.5, colour = "blue", label = "Cut-off point") +
  annotate("text", x = 2.9, y = 1.5, colour = "blue", label = "Sensory\nfailure") +
  annotate("text", x = 5, y = 5, colour = "red", label = "95% U-CI") +
  annotate("text", x = 2.5, y = 4, colour = "green", label = "95% L-CI")
#> Error in eval(expr, envir, enclos): object 'p' not found

Created on 2022-01-28 by the reprex package (v2.0.1)

What you've shared shows a clear issue with inability to load initial data.

If this does not reflect your actual issue you should clarify if you are trying to address some specific error message - you should always include the text of what that error message is, or if it produces a chart but not the chart you want, you should use words to describe the difference.but then if its an issue of that kind, you would do well to supply example data to make it easier to support you. dput()

Hope this helps.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.