R 将x轴更改为离散会改变边距

R 将x轴更改为离散会改变边距,r,ggplot2,R,Ggplot2,我用以下代码生成了一个绘图: plot <- ggplot(lmeans, aes(x=Day, y=value*100, group=variable, colour=variable)) + geom_point(aes(shape=variable), size=4) + geom_line(aes(linetype=variable), size=1.5) + ggtitle(paste("Nausea and Vomitting Frequencies by Day

我用以下代码生成了一个绘图:

plot <- ggplot(lmeans, aes(x=Day, y=value*100, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  ggtitle(paste("Nausea and Vomitting Frequencies by Day for", group_name)) +
  ylab("Frequency (%)") +
  ylim(0, 40) +
  theme(legend.title=element_blank()) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))
这就产生了:


我将
缩放x_离散()
更改为
缩放x_连续()
并使用
限制。这对你有用吗

ggplot(lmeans, aes(x=Day, y=value, group=variable, colour=variable)) +
geom_point(aes(shape=variable)) +
geom_line(aes(linetype=variable)) +
ylim(0, 10) +
scale_x_continuous(limits = c(-0.5, 4)) +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))

我将
缩放x_离散()
更改为
缩放x_连续()
并使用
限制。这对你有用吗

ggplot(lmeans, aes(x=Day, y=value, group=variable, colour=variable)) +
geom_point(aes(shape=variable)) +
geom_line(aes(linetype=variable)) +
ylim(0, 10) +
scale_x_continuous(limits = c(-0.5, 4)) +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))

在美学映射中使用
因子(天)

plot <- ggplot(lmeans, aes(x=factor(Day), y=value,
               group=variable, colour=variable)) +
  geom_point(aes(shape=variable)) +
  geom_line(aes(linetype=variable)) +
  ylim(0, 10) +
  labs(x="Day") +
  theme(legend.justification=c(1, 1), legend.position=c(1, 1))

print(plot)
绘图在美学绘图中使用
因子(天)

plot <- ggplot(lmeans, aes(x=factor(Day), y=value,
               group=variable, colour=variable)) +
  geom_point(aes(shape=variable)) +
  geom_line(aes(linetype=variable)) +
  ylim(0, 10) +
  labs(x="Day") +
  theme(legend.justification=c(1, 1), legend.position=c(1, 1))

print(plot)

plot不使用
scale\x\u discrete
并将
Day
设置为一个因子,绘图看起来正常:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))
其中:

使用
scale\u x\u discrete
时,可以包括
expand
参数以设置边距。例如:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  ylim(0, 10) +
  scale_x_discrete("Day", expand=c(0.05,0.1), breaks=c(0,1,2,3)) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))
其中:
如果不使用
缩放x_离散
并将
设置为一个因子,则绘图看起来正常:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))
其中:

使用
scale\u x\u discrete
时,可以包括
expand
参数以设置边距。例如:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  ylim(0, 10) +
  scale_x_discrete("Day", expand=c(0.05,0.1), breaks=c(0,1,2,3)) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))
其中:

您希望您的标签是什么?日期?你能给我们看一个你想要的例子吗?奇怪的行为,比如说,情节有正常的边距。你能提供一些示例数据吗?是的,请举例说明。我添加了一个可以复制的示例。你希望你的标签是什么?日期?你能给我们看一个你想要的例子吗?奇怪的行为,比如说,情节有正常的边距。你能提供一些示例数据吗?是的,请举个例子。我添加了一个可以复制的例子。