Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
R ggplot2-在x轴上显示特定值_R_Ggplot2 - Fatal编程技术网

R ggplot2-在x轴上显示特定值

R ggplot2-在x轴上显示特定值,r,ggplot2,R,Ggplot2,在使用ggplot2绘制线图时,我尝试在x轴上显示特定值。在我的表中,我有num值,它们彼此之间距离很远,这就是为什么我要将它们绘制为离散值 line <- ggplot(lineplot, aes(value,num, colour=attribute)) line + geom_line() x轴: 我得到的是: 0.003 0.6 0.9 我想: 0.003 0.6

在使用ggplot2绘制线图时,我尝试在x轴上显示特定值。在我的表中,我有num值,它们彼此之间距离很远,这就是为什么我要将它们绘制为离散值

line <- ggplot(lineplot, aes(value,num, colour=attribute))
line + geom_line()
x轴: 我得到的是:

0.003                                       0.6           0.9
我想:

0.003         0.6         0.9

尝试强制x轴作为因子而不是数字

line <- ggplot(lineplot, aes(factor(value),num, colour=attribute))
line + geom_line()

line如果希望将x轴视为离散因子,则必须添加
以告知ggplot2哪些点要与直线连接

df <- read.table(text = "num value   attribute
0   0.003   main
1   0.003   low
0   0.003   high
0   0.6 main
9   0.6 low
3   0.6 high
2   0.9 main
2   0.9 low
2   0.9 high", header = TRUE)

ggplot(df, aes(x = factor(value), y = num, group = attribute, color = attribute)) + 
  geom_line()

df从内部
aes()
删除
lineplot$
。我发现你的问题很不清楚。看看
?ggplot2::theme
参数
axis.text.x
可能合适。不清楚,你想在绘图之前使用自定义确实是Chase在中回答的问题,但我想用ggplot2做一个更清晰、可复制的示例可以帮助其他人。正确,但是,如果我这样做,我会丢失线(但不会丢失带有geom_point()的点)。我得到一个错误:“geom_路径:每个组只包含一个观察。你需要调整组美学吗?”@mightaskalot,当你添加组美学时,它会起作用。见“是”忘了放组aes。检查@Claus答案。
df <- read.table(text = "num value   attribute
0   0.003   main
1   0.003   low
0   0.003   high
0   0.6 main
9   0.6 low
3   0.6 high
2   0.9 main
2   0.9 low
2   0.9 high", header = TRUE)

ggplot(df, aes(x = factor(value), y = num, group = attribute, color = attribute)) + 
  geom_line()