簡體   English   中英

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 軸的scale_x_discrete(breaks = 1:10,labels= my_df$labels) 這是我借用的鏈接

有什么想法嗎?

您可以將x_val為數字並使用scale_x_continuous(breaks = 1:10,labels= my_df$labels)代替嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM