2017-03-08 1 views
1

그래프에 다음과 같이 변경하려고합니다.R 막대 그래프 변경

(1) 그래프의 제목이 중앙에 오길 원합니다. 제목을 추가하려고하면 왼쪽에 있습니다.

(2) y 축 레이블에 두 개 이상의 단어가 있어야합니다. 즉 "평균 (게임 당)"입니다. 하나 이상의 단어가있을 때마다 그래프가 완전히 바뀝니다. "평균"과 "(게임 당)"이 가능하다면 다른 라인에 올리기를 원할 것입니다.

(3) 나는 그래프의 배경이 회색이지만 흰색 그리드 선을 원한다.

도움이 될 것입니다.

df <- read.table(textConnection(
    'Statistic Warm Avg Cold 
    HR(Away) 1.151 1.028 .841 
    HR(Home) 1.202 1.058 .949 
    BB(Away) 3.205 3.269 3.481 
    BB(Home) 3.286 3.367 3.669 
    Runs(Away) 4.909 4.591 4.353 
    Runs(Home) 5.173 4.739 4.608'), header = TRUE) 

library(dplyr) 
library(tidyr) 
library(ggplot2) 

df %>% 
    gather(Temperature, Average, -Statistic) %>% 
    mutate(Temperature = factor(Temperature, c("Cold", "Avg", "Warm"))) %>% 
    ggplot(aes(x=Statistic, y=Average)) + 
    geom_col(aes(fill = Temperature), position = "dodge") + 
    scale_fill_manual(values = c("blue", "yellow", "red"))+ 
    theme_bw() + 
    theme(axis.title.y = element_text(angle = 0, vjust = 0.5)) 

답변

2

(1) 제목 y 축에 대한 레이블을 추가 할 테마

(2) 추가 labs(y = "Average\n(Per game)")plot.title = element_text(hjust = 0.5)를 추가 중심합니다. "\ n"은 줄을 끊습니다.

(3) 가장 쉬운 해결책은 theme_bw을 제거하는 것입니다. 또한,

df <- read.table(textConnection(
    'Statistic Warm Avg Cold 
    HR(Away) 1.151 1.028 .841 
    HR(Home) 1.202 1.058 .949 
    BB(Away) 3.205 3.269 3.481 
    BB(Home) 3.286 3.367 3.669 
    Runs(Away) 4.909 4.591 4.353 
    Runs(Home) 5.173 4.739 4.608'), header = TRUE) 

library(dplyr) 
library(tidyr) 
library(ggplot2) 

df %>% 
    gather(Temperature, Average, -Statistic) %>% 
    mutate(Temperature = factor(Temperature, c("Cold", "Avg", "Warm"))) %>% 
    ggplot(aes(x=Statistic, y=Average)) + 
    geom_bar(aes(fill = Temperature), stat='identity', position = "dodge") + 
    scale_fill_manual(values = c("blue", "yellow", "red"))+ 
    theme(axis.title.y = element_text(angle = 0, vjust = 0.5), 
     plot.title = element_text(hjust = 0.5)) + 
    labs(title = "Title", y = "Average\n(Per game)") 
+0

감사합니다! 내가 원하는 방식으로 보입니다. –

1

NBATreands '대답은 완벽 http://docs.ggplot2.org/dev/vignettes/themes.html을 확인하고이 내 대답입니다 :

df %>% 
    gather(Temperature, Average, -Statistic) %>% 
    mutate(Temperature = factor(Temperature, c("Cold", "Avg", "Warm"))) %>% 
    ggplot(aes(x=Statistic, y=Average)) + 
    ggtitle("This is the title") + 
    ylab("Average\n(Per game)") + 
    geom_col(aes(fill = Temperature), position = "dodge") + 
    scale_fill_manual(values = c("blue", "yellow", "red"))+ 
    theme(
    plot.title = element_text(hjust=0.5), 
    axis.title.y = element_text(angle = 0, vjust = 0.5), 
    panel.background = element_rect(fill = "gray"), 
    panel.grid = element_line(colour = "white") 
    ) 

결과 플롯은 다음과 같습니다 enter image description here

+0

나는 배경이 회색의 그늘을 좋아한다! –