移除由R中的geom_smooth引起的趋势线。

huangapple go评论60阅读模式
英文:

Remove the trend line caused by geom_smooth in R

问题

我试图移除使用geom_smooth创建的趋势线。期望只获取置信区间而不包括趋势线。

我不确定该如何处理,有人能帮助我吗?

我正在使用的数据框架是:

structure(list(Date = structure(c(19389, 19417, 19448, 19478, 
19509, 19539, 19570, 19601, 19631, 19662, 19692, 19723, 19754, 
19783, 19814, 19844, 19875, 19905), class = "Date"), Qty = c(34810, 
34963.3333333333, 35513.3333333333, 36946.6666666667, 38803.3333333333, 
38618.3333333333, 40798.3333333333, 42645, 44686.6666666667, 
46246.6666666667, 43283.3333333333, 40358.3333333333, 39831.6666666667, 
40233.3333333333, 40680, 41103.3333333333, 41446.6666666667, 
41683.3333333333)), row.names = 2:19, class = "data.frame")

我为此编写的代码如下:

Subset <- read_excel("C:/X/X/X/File.xlsx")

ggplot(Subset,aes(x=Date,y=`Qty`))+
  geom_point(color="purple",size=4,pch=20) +
  geom_line(linetype="dashed") +
  geom_smooth()

需要移除我标记的蓝色趋势线以更好理解。

英文:

I am trying to remove the trend line which is created while using geom_smooth. The expectation is to just get the confidence interval without the trend line.

I am not sure how to go about with it, can someone help me out?

The data frame I am working with is:

structure(list(Date = structure(c(19389, 19417, 19448, 19478, 
19509, 19539, 19570, 19601, 19631, 19662, 19692, 19723, 19754, 
19783, 19814, 19844, 19875, 19905), class = &quot;Date&quot;), Qty = c(34810, 
34963.3333333333, 35513.3333333333, 36946.6666666667, 38803.3333333333, 
38618.3333333333, 40798.3333333333, 42645, 44686.6666666667, 
46246.6666666667, 43283.3333333333, 40358.3333333333, 39831.6666666667, 
40233.3333333333, 40680, 41103.3333333333, 41446.6666666667, 
41683.3333333333)), row.names = 2:19, class = &quot;data.frame&quot;)

The code I wrote for this is given below:

Subset &lt;- read_excel(&quot;C:/X/X/X/File.xlsx&quot;)

ggplot(Subset,aes(x=Date,y=`Qty`))+
  geom_point(color=&quot;purple&quot;,size=4,pch=20) +
  geom_line(linetype=&quot;dashed&quot;) +
  geom_smooth()

The plot that arises due to this is given below:

移除由R中的geom_smooth引起的趋势线。

Need to remove the blue trend line which I have marked for better understanding.

答案1

得分: 2

使用 linetype = "blank"

Aesthetic specifications中,

线型可以通过以下方式指定:

整数或名称:0 = 空白, 1 = 实线, 2 = 虚线, 3 = 点线, 4 = 点虚线, 5 = 长虚线, 6 = 双虚线

library(ggplot2)

Subset <-
	structure(list(
		Date = structure(
			c(
				19389,
				19417,
				19448,
				19478,
				19509,
				19539,
				19570,
				19601,
				19631,
				19662,
				19692,
				19723,
				19754,
				19783,
				19814,
				19844,
				19875,
				19905
			),
			class = "Date"
		),
		Qty = c(
			34810,
			34963.3333333333,
			35513.3333333333,
			36946.6666666667,
			38803.3333333333,
			38618.3333333333,
			40798.3333333333,
			42645,
			44686.6666666667,
			46246.6666666667,
			43283.3333333333,
			40358.3333333333,
			39831.6666666667,
			40233.3333333333,
			40680,
			41103.3333333333,
			41446.6666666667,
			41683.3333333333
		)
	),
	row.names = 2:19,
	class = "data.frame")

ggplot(Subset, aes(x = Date, y = `Qty`)) +
	geom_point(color = "purple",
			   size = 4,
			   pch = 20) +
	geom_line(linetype = "dashed") +
	geom_smooth(linetype = "blank")

移除由R中的geom_smooth引起的趋势线。

英文:

Use linetype = &quot;blank&quot;.

From the Aesthetic specifications,

> Line types can be specified with:
>
> An integer or name: 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4
> = dotdash, 5 = longdash, 6 = twodash

library(ggplot2)

Subset &lt;-
	structure(list(
		Date = structure(
			c(
				19389,
				19417,
				19448,
				19478,
				19509,
				19539,
				19570,
				19601,
				19631,
				19662,
				19692,
				19723,
				19754,
				19783,
				19814,
				19844,
				19875,
				19905
			),
			class = &quot;Date&quot;
		),
		Qty = c(
			34810,
			34963.3333333333,
			35513.3333333333,
			36946.6666666667,
			38803.3333333333,
			38618.3333333333,
			40798.3333333333,
			42645,
			44686.6666666667,
			46246.6666666667,
			43283.3333333333,
			40358.3333333333,
			39831.6666666667,
			40233.3333333333,
			40680,
			41103.3333333333,
			41446.6666666667,
			41683.3333333333
		)
	),
	row.names = 2:19,
	class = &quot;data.frame&quot;)

ggplot(Subset, aes(x = Date, y = `Qty`)) +
	geom_point(color = &quot;purple&quot;,
			   size = 4,
			   pch = 20) +
	geom_line(linetype = &quot;dashed&quot;) +
	geom_smooth(linetype = &quot;blank&quot;)

移除由R中的geom_smooth引起的趋势线。

答案2

得分: 1

一种方法是将 colour 设置为 "transparent"

library(ggplot2)

ggplot(df, aes(x = Date, y = `Qty`)) +
  geom_point(color = "purple", size = 4, pch = 20) +
  geom_line(linetype = "dashed") +
  geom_smooth(se = TRUE, colour = "transparent")

移除由R中的geom_smooth引起的趋势线。

英文:

One way is to set colour to "transparent"

library(ggplot2)

ggplot(df,aes(x=Date,y=`Qty`))+
  geom_point(color=&quot;purple&quot;,size=4,pch=20) +
  geom_line(linetype=&quot;dashed&quot;) +
  geom_smooth(se = TRUE, colour = &quot;transparent&quot;)

移除由R中的geom_smooth引起的趋势线。

答案3

得分: 0

这可以通过将平滑的颜色设置为NA来实现,即:

ggplot(Subset, aes(x = Date, y = `Qty`)) +
  geom_point(color = "purple", size = 4, pch = 20) +
  geom_line(linetype = "dashed") +
  geom_smooth(colour = NA)

移除由R中的geom_smooth引起的趋势线。

英文:

This can be acheived by setting the smoother colour to NA, i.e.:

ggplot(Subset,aes(x=Date,y=`Qty`))+
  geom_point(color=&quot;purple&quot;,size=4,pch=20) +
  geom_line(linetype=&quot;dashed&quot;) +
  geom_smooth(colour=NA)

移除由R中的geom_smooth引起的趋势线。

huangapple
  • 本文由 发表于 2023年3月9日 18:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定