0
library(ggplot2)
x=letters[1:3]
y=1:3
qplot(x, y)
qplot(x, y, geom=c('point', 'line'))

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

I want to connect lines between the points. But when the x is a string, the above commands won't work. It works when the x is numeric. I'd think qplot should be made more user-friendly in this case.

How to make it connect the points with lines when x is a string?

stefan
  • 90,330
  • 6
  • 25
  • 51
user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 1
    Try with `qplot(x, y, group = 1, geom=c('point', 'line'))`. See [ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"](https://stackoverflow.com/a/29019102/12993861) – stefan Mar 07 '22 at 07:12
  • Where the group option is documented in qplot. I don't find it in `?qplot`. – user1424739 Mar 08 '22 at 03:04
  • I think you should add group=1 as the best answer here instead of closing this question. The link that you provided is full of junk answers. – user1424739 Mar 08 '22 at 03:05

1 Answers1

0

One solution is provided by @stefan. Another one could be the following.

Sample data:

x=letters[1:3]
y=1:3

Sample code:

    d <- data.frame(x, y) %>%
      mutate(x = x %>% 
       factor(levels = x))

library(ggplot2)

    ggplot(data = d, aes(x = x, y = y, group = 1)) +
      geom_line() +
      scale_x_discrete(labels = x, breaks = x) 

Plot:

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21