2017-09-20 10 views
0

파티 편광 (something like this)에 대한 일부 데이터를 조사 중이며 geom_dumbbellggaltggplot2입니다. 계속해서 동일한 aes 오류 및 포럼의 다른 솔루션이이를 효과적으로 처리하지 못했습니다. 이것은 나의 샘플 데이터입니다. 미학은 길이가 1이거나 데이터 (1)과 같아야합니다. x, y, 레이블

df <- data_frame(policy=c("Not enough restrictions on gun ownership", "Climate change is an immediate threat", "Abortion should be illegal"), 
      Democrats=c(0.54, 0.82, 0.30), 
      Republicans=c(0.23, 0.38, 0.40), 
      diff=sprintf("+%d", as.integer((Democrats-Republicans)*100))) 

는 그래서, 플롯의 순서를 유지하고 싶었 factor로 정책을 전환하고 첫 번째 줄에 표시 할 %를 원했다.
df <- arrange(df, desc(diff)) 
    df$policy <- factor(df$policy, levels=rev(df$policy)) 

    percent_first <- function(x) { 
    x <- sprintf("%d%%", round(x*100)) 
    x[2:length(x)] <- sub("%$", "", x[2:length(x)]) 
    x 
    } 

은 그 때 나는 내가 원하는 것을 가까이에 뭔가를 렌더링 ggplot을 사용했다.

gg2 <- ggplot() 
    gg2 <- gg + geom_segment(data = df, aes(y=country, yend=country, x=0, xend=1), color = "#b2b2b2", size = 0.15) 

    # making the dumbbell 
    gg2 <- gg + geom_dumbbell(data=df, aes(y=country, x=Democrats, xend=Republicans), 
        size=1.5, color = "#B2B2B2", point.size.l=3, point.size.r=3, 
        point.color.l = "#9FB059", point.color.r = "#EDAE52") 

나는 다음 두 점 (like this를) 레이블 위에 DemocratRepublican을 읽기 위해 아령을 원했다. 이것은 오류가 발생하는 곳입니다.

gg2 <- gg + geom_text(data=filter(df, country=="Government will not control gun violence"), 
        aes(x=Democrats, y=country, label="Democrats"), 
        color="#9fb059", size=3, vjust=-2, fontface="bold", family="Calibri") 
    gg2 <- gg + geom_text(data=filter(df, country=="Government will not control gun violence"), 
       aes(x=Republicans, y=country, label="Republicans"), 
       color="#edae52", size=3, vjust=-2, fontface="bold", family="Calibri") 

내가 잘못하고있는 것에 대한 의견이 있으십니까?

+2

곳 '에서 오는 country'는 무엇입니까? – Nate

+2

'gg' 또는'gg2'를 참조하려고합니까?! – bouncyball

+0

'aes' 호출은'data'에 정의 된 변수를 받아들입니다.'label = "Republicans"'변수는 아니지만 상수 문자열입니다. 따라서 오류 – missuse

답변

0

geom_segment()geom_point()으로 자신의 "덤벨"을 만드는 것이 더 쉬울 것이라고 생각합니다. 당신의 df 작업과 "정책"에 변수 참조 문헌 "국가"를 변경 : 일부 중복 레이블을 가지고

enter image description here :

library(tidyverse) 

# gather data into long form to make ggplot happy 
df2 <- gather(df,"party", "value", Democrats:Republicans) 

ggplot(data = df2, aes(y = policy, x = value, color = party)) + 
    # our dumbell 
    geom_path(aes(group = policy), color = "#b2b2b2", size = 2) + 
    geom_point(size = 7, show.legend = FALSE) + 
    # the text labels 
    geom_text(aes(label = party), vjust = -1.5) + # use vjust to shift text up to no overlap 
    scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red")) + # named vector to map colors to values in df2 
    scale_x_continuous(limits = c(0,1), labels = scales::percent) # use library(scales) nice math instead of pasting 

이 플롯을 생성합니다.

ggplot(data = df2, aes(y = policy, x = value, color = party)) + 
    geom_path(aes(group = policy), color = "#b2b2b2", size = 2) + 
    geom_point(size = 7, show.legend = FALSE) + 
    geom_text(aes(label = gsub("^(\\D).*", "\\1", party)), vjust = -1.5) + # just the first letter instead 
    scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red"), 
         guide = "none") + 
    scale_x_continuous(limits = c(0,1), labels = scales::percent) 

만 이름을 맨 위에 문제를 레이블 : 난 당신이 다음과 같이 당사자의 단지 첫 글자를 사용하는 경우 피하기 수 있다고 생각

ggplot(data = df2, aes(y = policy, x = value, color = party)) + 
    geom_path(aes(group = policy), color = "#b2b2b2", size = 2) + 
    geom_point(size = 7, show.legend = FALSE) + 
    geom_text(data = filter(df2, policy == "Not enough restrictions on gun ownership"), 
        aes(label = party), vjust = -1.5) + 
    scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red")) + 
    scale_x_continuous(limits = c(0,1), labels = scales::percent) 
+0

감사합니다. 내 원래의 생각은 첫 번째 행의 상단에 레이블이 있고 나머지는 색상 코드로 처리했습니다. – sdiya

+0

물론 첫 번째 행에 라벨을 지정하려는 경우 몇 가지 옵션이 있습니다. 답변에 하나를 추가 할 것입니다. – Nate

관련 문제