0

The following works fine:

my_df <- data.frame(x_val = 1:10, y_val = sample(1:20,10), 
            labels = sample(c("a", "b"), 10, replace = T))
ggplot(data = my_df, aes(x = x_val, y = y_val)) + geom_line()

but if I chance x_val to factor, I am getting blank plot and message:

my_df <- data.frame(x_val = 1:10, y_val = sample(1:20,10), 
          labels = sample(c("a", "b"), 10, replace = T))
my_df$x_val <- as.factor(my_df$x_val)
ggplot(data = my_df, aes(x = x_val, y = y_val)) + geom_line()

message:

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

I can obviously drop factor conversion, but I need it in order to replace labels of x axis with scale_x_discrete(breaks = 1:10,labels= my_df$labels). Here is where I borrowed it link

Any thoughts?

1 Answer 1

2

Can you just leave x_val as numeric and use scale_x_continuous(breaks = 1:10,labels= my_df$labels) instead?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked, I also found that adding group= 1 helps. aes(x = x_val, y = y_val, group = 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.