ggplot 缺少带有 x 轴因子的图
ggplot missing plot with x-axis factor
以下工作正常:
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()
但如果我有机会 x_val
考虑因素,我将得到空白图和消息:
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()
留言:
geom_path: Each group consists of only one observation. Do you
need to adjust the group aesthetic?
我显然可以放弃 factor
转换,但我需要它来用 scale_x_discrete(breaks = 1:10,labels= my_df$labels)
替换 x 轴的标签。这是我借的地方link
有什么想法吗?
您能否将 x_val
保留为数字并使用 scale_x_continuous(breaks = 1:10,labels= my_df$labels)
代替?
以下工作正常:
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()
但如果我有机会 x_val
考虑因素,我将得到空白图和消息:
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()
留言:
geom_path: Each group consists of only one observation. Do you
need to adjust the group aesthetic?
我显然可以放弃 factor
转换,但我需要它来用 scale_x_discrete(breaks = 1:10,labels= my_df$labels)
替换 x 轴的标签。这是我借的地方link
有什么想法吗?
您能否将 x_val
保留为数字并使用 scale_x_continuous(breaks = 1:10,labels= my_df$labels)
代替?