이것은 ggplot2에 대한 나의 첫 번째 시도이며 어려움을 겪고 있습니다. 두 가지 모두에 대한 선형 회귀를 표시하면서 증가 된 x 축에 대해 두 개의 일련의 난수를 플로팅하려고합니다. 지금까지 산점도를 그리는 데 성공했지만 회귀선에서 계속 오류가 발생합니다. 나는 그것이 가능하다는 것을 알고 있지만 아이디어를 실행하기 위해 무언가를 놓치고 있습니다. RStudio Desktop 버전 1.3.1056, Water Lily (tidyverse가로드 된 상태)를 실행하고 있습니다.
나는 이것이 산점도를 표시하는 데 작동한다는 것을 알고 있습니다 (제안되는 경우 더 우아한 변형에 열려 있습니다).
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') # Title and axes labels
또한 하나의 y- 시리즈 만 사용하는 경우 선형 회귀선을 표시하는 데 효과적이라는 것을 알고 있습니다.
ggplot(a) +
aes(x = Datapoint, y = value, color = variable) +
geom_point()
geom_smooth()
또는 geom_smooth(method = lm)
메인 블록에 추가하려고하면 "Error in FUN (X [[i]], ...) : object 'value'not found"메시지가 표시됩니다. 예를 들면 다음과 같습니다.
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
geom_smooth(method = lm)
결과는 다음과 같습니다.
> ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
+ geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
+ geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
+ labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
+ geom_smooth(method = lm)
Error in FUN(X[[i]], ...) : object 'value' not found
영감을 얻은 곳은 다음과 같습니다.
나는 이것이 간단한 문제라고 확신하지만 아직 이해하지 못하는 문제입니다. 내가 무엇을 놓치고 있습니까?
내가 사용중인 데이터 파일은 https://github.com/davidmvermillion/Chart_Comparisons/blob/master/Seeded_Values_for_Comparison_Project.csv에서 호스팅됩니다.
이것은 내 현재 R 파일입니다 : https://github.com/davidmvermillion/Chart_Comparisons/blob/master/ggplot2Demo.R
감사합니다!
이 예제를 시도하십시오. 나는 당신이 원하는 것에 가깝다고 믿습니다. 다음에를 사용하여 적절한 형식으로 문제를 재현 할 수있는 데이터를 포함 해주세요 dput()
. 데이터에 일부 변수가 누락되었거나 잘못된 이름을 입력 한 것 같습니다. 이 예제는 시작하기에 좋은 지점이 될 수 있습니다 (또한 github의 실제 데이터를 사용하는 일부 솔루션도 포함됨).
library(tidyverse)
#Data
data("iris")
#Code for data and plot
iris %>%
ggplot(aes(x=Sepal.Length,y=Sepal.Width,group=Species,color=Species))+
geom_point()+
geom_smooth(method = 'lm',se=F)
산출:
또는 패싯 (각 그룹에 대한 플롯)을 원하는 경우 다음 코드를 시도하십시오.
#Code for data and plot 2
iris %>%
ggplot(aes(x=Sepal.Length,y=Sepal.Width,group=Species,color=Species))+
geom_point()+
geom_smooth(method = 'lm',se=F)+
facet_wrap(.~Species)
산출:
그리고 github에서 데이터를 탐색 한 후 다음을 찾고있을 수 있습니다 (힌트 : DataPoint를 오래 유지하기 위해 데이터 모양 변경).
#Code for data and plot 3
df %>% pivot_longer(-Datapoint) %>%
ggplot(aes(x=Datapoint,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = 'lm',se=F)
산출:
또는 패싯이있는 더 좋은 솔루션 :
#Code for data and plot 4
df %>% pivot_longer(-Datapoint) %>%
ggplot(aes(x=Datapoint,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = 'lm')+
facet_wrap(.~name)
산출:
사용 된 일부 데이터 :
#Data
df <- structure(list(Datapoint = 1:50, Series1 = c(37L, 7L, 26L, 27L,
91L, 77L, 58L, 87L, 58L, 13L, 62L, 91L, 18L, 18L, 23L, 61L, 90L,
26L, 2L, 54L, 27L, 30L, 52L, 39L, 3L, 37L, 32L, 43L, 28L, 6L,
50L, 50L, 71L, 45L, 37L, 19L, 84L, 61L, 46L, 51L, 39L, 95L, 16L,
27L, 28L, 89L, 54L, 98L, 98L, 61L), Series2 = c(25L, 88L, 65L,
5L, 28L, 51L, 29L, 83L, 10L, 98L, 52L, 26L, 68L, 64L, 3L, 6L,
39L, 53L, 96L, 15L, 40L, 24L, 65L, 27L, 84L, 13L, 83L, 43L, 14L,
65L, 76L, 95L, 15L, 100L, 5L, 62L, 92L, 58L, 10L, 32L, 9L, 83L,
41L, 99L, 46L, 32L, 19L, 1L, 13L, 39L)), class = "data.frame", row.names = c(NA,
-50L))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다