【发布时间】:2016-05-09 17:47:47
【问题描述】:
我有一个像这样的基本数据框,我正在尝试绘制 x 与 y 的情节。出于某种原因,它会将我的 x 值转换为数字(连续比例)和绘图,而不是将它们保留为因子。知道它为什么会这样做以及我能做些什么来使其表现不同(正确地)吗?
df <- data.frame(x = c(1, 2, 4, 8, 16, 32), y = 1:6)
df$x <- as.factor(df$x)
str(df)
'data.frame': 6 obs. of 2 variables:
$ x: Factor w/ 6 levels "1","2","4","8",..: 1 2 3 4 5 6
$ y: int 1 2 3 4 5 6
levels(df$x)
[1] "1" "2" "4" "8" "16" "32"
使用ggplot 进行绘图,这显然是正确的:
library(ggplot2)
ggplot(df, aes(x = x, y = y)) + geom_point()
使用plotly R 绘图,将 x 视为连续值:
library(plotly)
plot_ly(df, x = x, y = y, mode = 'markers')
【问题讨论】:
-
作为替代方案,您可以使用 ggplotly()