1

I've created a 2x2 data frame for plotting a single line comparing 2 the outcome of 2 treatment. The code I used for this actually does yield a plot with the line but the x limits does not allow to see the x labels, which contains a factor variable with only 2 categories.

I tried changing the names of the levels in the factor, changing the xlim in the ggplot() function, putting different values (even characters), checking my luck using geom_abline(), but nothing has worked.

library(ggplot2)
quien <- factor(c(1,2))
names(quien) <- c("Solo", "Con amigos")
likes<-c(4.3,3.8)
selfie <- data.frame(quien, likes)

ggplot(data=selfie, aes(x=quien, y=y))+
    geom_line()+
    ylim(0,5)

The expected outcome is a plot with a line that joins the coordinates ("Solo, 4.3) and ("Con amigos", 3.8), as we see it in a comparison for two groups of an Experiment with one single factor and 2 treatments.

The code I showed before yields the next error message:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

1 Answers1

0

If I am understanding you correctly, you simply need to add a group command to your aes function (see here). A clarfying point: ggplot requires your X-axis to be a value what you are directly calling - typically a value within a data frame. The code shown here has (what I think is) your intended X-axis as row names, which you are not accessing. And a quick FYI: the code provided doesn't produce your described error.

Anyways, is this what you are looking for?

library(ggplot2)
likes<-c(4.3,3.8)
selfie <- data.frame(quien, likes)

selfie$name <- c("Solo","Con amigos")


ggplot(data=selfie, aes(x=name, y = likes, group = 1))+
  geom_line() +
  ylim(0,5)
Peter_Evan
  • 777
  • 8
  • 15