Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scale_x_discrete()删除图形的左侧(从0到13)_R_Graphics_Ggplot2 - Fatal编程技术网

使用scale_x_discrete()删除图形的左侧(从0到13)

使用scale_x_discrete()删除图形的左侧(从0到13),r,graphics,ggplot2,R,Graphics,Ggplot2,可在此处下载数据: 这是我的密码 # load data raw_data <- read.csv("Sleep vs reaction time (Responses) - Form Responses 1.csv") library(ggplot2) #histogram qplot(x = Age, data = raw_data, xlim = c(13,43), geom = "histogram") + scale_x_continuous() qplot(x = Ag

可在此处下载数据:

这是我的密码

# load data
raw_data <- read.csv("Sleep vs reaction time (Responses) - Form Responses 1.csv")

library(ggplot2)

#histogram
qplot(x = Age, data = raw_data, xlim = c(13,43), geom = "histogram") + scale_x_continuous()


qplot(x = Age, data = raw_data, xlim = c(13,43), geom = "histogram") + scale_x_discrete()
结果:

您应该让数据的类别确定刻度是离散的还是连续的
ggplot
没有内置的对整数刻度的支持,因为整数刻度不同于数字刻度,因此如果您想要离散刻度,您应该将您的年龄数据转换为
因子
(如果尚未转换):

如果不指定
xlim
,则默认值将为您提供所需的内容

qplot(x = Age_factor, data = raw_data, geom = "histogram")

这有点令人困惑,但实际上是您的
xlim=c(13,43)
将图形向右移动。在离散量表上,13和43指的是第13和43个离散水平,因此通过设置这些xlim,您将迫使数据向右移动。

Gregor,这不应该是
x=Age\u factor
?此外,您可以只执行
x=factor(Age)
操作,而不必在数据框中创建新列。感谢@eipi10,
Age\u factor
已更正。我更喜欢将数据转换为内部(隐含的)
aes()
,尤其是当我还没有下载OP的数据来检查它时。
raw_data$Age_factor = factor(raw_data$Age)
qplot(x = Age_factor, data = raw_data, geom = "histogram")