2016-12-06 82 views
1

我使用qplotggplot2做散点图。我无法看到x轴上的所有值。另外,它将从x轴移除NA。如何保留NA并控制X轴显示的特征数量?不在ggplot2散点图中显示使用qplot

rate_plot = qplot(Result$temp, Result$CR, main="Rate", xlab=feature, ylab="Rate", size=I(3))+ 
    scale_x_discrete(drop=FALSE) 

Plot looks like this

数据: Google Docs link

Result <- read.table(text = " temp NCH type CH i.type CR 
1 NA 1878464 nochurn 549371 churn 0.226280204 
2 1.87 2236 nochurn 4713 churn 0.678227083 
3 2.14 4945 nochurn 8530 churn 0.633024119 
4 2.25 423 nochurn 972 churn 0.696774194 
5 2.79 3238 nochurn 7692 churn 0.703751144 
6 3.25 266817 nochurn 12678 churn 0.045360382 
7 3.33 2132 nochurn 4295 churn 0.668274467 
8 5.1 6683 nochurn 7743 churn 0.536739221 
9 6 342554 nochurn 21648 churn 0.059439542 
10 6.51 1785 nochurn 4764 churn 0.727439304 
11 8 13668 nochurn 22751 churn 0.624701392 
12 9.85 6005 nochurn 14687 churn 0.709791224 
13 11.99 378 nochurn 850 churn 0.69218241", header = TRUE) 
+1

你是什么意思“无法看到在x轴的所有值”?也许看到[scale_x_continuous](http://docs.ggplot2.org/0.9.3/scale_continuous.html) – zx8754

+0

谢谢你指点我正确的方向。我现在可以在我的x轴上添加更多标签。这解决了其中一个问题。 – Shivendra

+0

它解决了什么问题? – Gregor

回答

1

对于自定义蜱和标签,我们可以使用scale_x_continuous

下面警告装置与NA值的行被从绘图数据中删除:

除去含有缺失值(geom_point)

解决方法,使NA显示在x轴1行,我们需要为NA值指定一些值,这里我在绘图右端绘制NA值。获取xaxis的最大值(temp变量),然后使用自定义x轴标签。

library(ggplot2) 

# set NA to max value + 1 
plotDat <- Result 
plotDat[ is.na(plotDat$temp), "temp"] <- max(ceiling(plotDat$temp), na.rm = TRUE) + 1 

#plot with custom breaks and labels 
ggplot(plotDat, aes(x = temp, y = CR)) + 
    geom_point() + 
    scale_x_continuous(breaks = 1:max(ceiling(plotDat$temp)), 
        labels = c(1:(max(ceiling(plotDat$temp)) - 1), "NA")) 

enter image description here

+0

虽然这可以起作用,但我的实际代码可以生成具有巨大变化的x轴范围的500多个功能的图形,而'ggplot2'可以照顾并显示相应的标签。如果我把范围'1:n',它总是会填满所有的标签,并且对于一些范围会变得非常混乱。 'scale_x_continuous(na.value = TRUE)'保持NA值,只在x轴上不显示。 – Shivendra

+0

@Shivendra'na.value'参数需要一个数字。当我们将它设置为“TRUE”时,它将被转换为“1”并且正在绘制。 – zx8754

+0

噢,是的,我在这里看到问题。我想要一个非数字以连续的尺度显示。 对于某些“NA”值显示的特征。这意味着'ggplot2'将它们当作'factor',而在这种情况下,它将它们视为'连续的',因此我面临着困难。 – Shivendra