1
我有一些数据,我正在尝试使用ggplot2进行图形化,其中x轴是数值/整数值。绘制图形时,我希望图形只显示数据集中存在的x的值,而不会将值添加到x轴(离散值)。下面的完全可重现的示例演示了这个问题:即使提供的x轴值为1,3,25,生成的图形在x轴上呈现0,5,15,20,25。我尝试过铸造这些价值观,以及尝试一种独立的规模,但没有一个似乎有效。ggplot2 - 来自数值/整数数据的离散X轴值
编辑尽管x轴上的值是数字/整数,但它们代表因素(即试验中的人数,发动机中的汽缸数等)并且不是连续值。
#Example
library(ggplot2)
row1 <- c(1, 1)
row2 <- c(3, 2)
row3 <- c(25, 10)
data <- data.frame()
data <- rbind(data, row1)
data <- rbind(data, row2)
data <- rbind(data, row3)
names(data) <- c("A", "B")
qplot(A, B, data = data, geom="line")
#Things Tried
qplot(factor(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(as.factor(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(character(A), B, data = data, geom="line") #Error in character(A) : invalid 'length' argument
qplot(as.character(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(A, B, data = data, geom="line") + scale_x_discrete(breaks = data$A) #Works, but values are too far apart
感谢您的回复。这个解决方案效果很好,但它可以使用qplot函数来完成,而不是使用ggplot2函数吗? qplot创建更清晰的脚本,所以我尽量坚持下去。此外,重新:误导性表述:请参阅我的原始问题中的编辑。 – lolcodez 2014-12-02 23:47:04
@lolcodez了解,但数字暗示数字关系。我会列出他们作为审判1,审判2,审判25等...或类似的东西。我已将qplot代码添加到我的解决方案中。 – 2014-12-02 23:52:03