programing

ggplot2 꺽은선형 차트는 "geom_path: 각 그룹은 하나의 관측치로만 구성됩니다.그룹 에스테틱을 조정할 필요가 있습니까?"

telebox 2023. 6. 6. 08:11
반응형

ggplot2 꺽은선형 차트는 "geom_path: 각 그룹은 하나의 관측치로만 구성됩니다.그룹 에스테틱을 조정할 필요가 있습니까?"

이 데이터 프레임("df"):

year pollution
1 1999 346.82000
2 2002 134.30882
3 2005 130.43038
4 2008  88.27546

다음과 같은 꺽은선형 차트를 만들려고 합니다.

  plot5 <- ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore")

다음과 같은 오류가 발생합니다.

gem_path: 각 그룹은 하나의 관측치로만 구성됩니다.당신은 그룹 에스테틱을 조정해야 합니까?

꺽은선형 차트를 원해도 차트가 산점도로 나타납니다.교체하려고 했습니다.geom_line()와 함께geom_line(aes(group = year))하지만 그것은 효과가 없었습니다.

답변에서 저는 연도를 요인 변수로 변환하라는 말을 들었습니다.저는 했고 문제는 계속됩니다.다음은 의 출력입니다.str(df)그리고.dput(df):

'data.frame':   4 obs. of  2 variables:
 $ year     : num  1 2 3 4
 $ pollution: num [1:4(1d)] 346.8 134.3 130.4 88.3
  ..- attr(*, "dimnames")=List of 1
  .. ..$ : chr  "1999" "2002" "2005" "2008"

structure(list(year = c(1, 2, 3, 4), pollution = structure(c(346.82, 
134.308821199349, 130.430379885892, 88.275457392443), .Dim = 4L, .Dimnames = list(
    c("1999", "2002", "2005", "2008")))), .Names = c("year", 
"pollution"), row.names = c(NA, -4L), class = "data.frame")

추가만 하면 됩니다.group = 1ggplot 또는 gem_line aes로 이동합니다.

선 그래프의 경우 연결할 점을 알 수 있도록 데이터 점을 그룹화해야 합니다.이 경우 모든 점이 연결되어야 하므로 그룹=1입니다.더 많은 변수가 사용되고 여러 개의 선이 그려지면 일반적으로 변수별로 선의 그룹화가 이루어집니다.

참조:R에 대한 요리책: 그래프 막대_and_line_graphs_(ggplot2), 선 그래프.

사용해 보십시오.

plot5 <- ggplot(df, aes(year, pollution, group = 1)) +
         geom_point() +
         geom_line() +
         labs(x = "Year", y = "Particulate matter emissions (tons)", 
              title = "Motor vehicle emissions in Baltimore")

변수 중 하나가 실제로 요인 변수이기 때문에 이 오류가 발생합니다.실행

str(df) 

이것을 확인하기 위해.그런 다음 이 이중 변수를 변경하여 "1,2,3,4" 수준 번호로 변환하는 대신 연도 번호를 유지합니다.

df$year <- as.numeric(as.character(df$year))

편집: data.frame에 pb를 발생시킬 수 있는 클래스 "array" 변수가 있는 것 같습니다.시도해 보십시오.

df <- data.frame(apply(df, 2, unclass))

그리고 또 음모를 꾸미나요?

데이터 프레임에도 비슷한 문제가 있었습니다.

group time weight.loss
1 Control  wl1    4.500000
2    Diet  wl1    5.333333
3  DietEx  wl1    6.200000
4 Control  wl2    3.333333
5    Diet  wl2    3.916667
6  DietEx  wl2    6.100000
7 Control  wl3    2.083333
8    Diet  wl3    2.250000
9  DietEx  wl3    2.200000

gom_line이 점을 연결하여 선을 그리는 방법을 알 수 있도록 x축에 대한 변수는 숫자여야 한다고 생각합니다.

두 번째 열을 숫자로 변경한 후:

 group time weight.loss
1 Control    1    4.500000
2    Diet    1    5.333333
3  DietEx    1    6.200000
4 Control    2    3.333333
5    Diet    2    3.916667
6  DietEx    2    6.100000
7 Control    3    2.083333
8    Diet    3    2.250000
9  DietEx    3    2.200000

그러면 작동합니다.

새 세션에서 R을 시작하고 다음 위치에 붙여넣습니다.

library(ggplot2)

df <- structure(list(year = c(1, 2, 3, 4), pollution = structure(c(346.82, 
134.308821199349, 130.430379885892, 88.275457392443), .Dim = 4L, .Dimnames = list(
    c("1999", "2002", "2005", "2008")))), .Names = c("year", 
"pollution"), row.names = c(NA, -4L), class = "data.frame")

df[] <- lapply(df, as.numeric) # make all columns numeric

ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", 
                y = "Particulate matter emissions (tons)", 
                title = "Motor vehicle emissions in Baltimore")

저도 비슷한 프롬프트를 받았습니다.그 이유는 x축을 몇 퍼센트(예: 10%A, 20%B 등) 단위로 지정했기 때문입니다. 따라서 다른 접근 방식으로는 이러한 값을 곱하여 가장 간단한 형태로 작성할 수 있습니다.

표시된 대부분의 데이터가 축 한계를 벗어나는 경우에도 이러한 현상이 발생할 수 있습니다.이 경우 축 눈금을 적절히 조정합니다.

언급URL : https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation

반응형