2016-04-16 90 views
1

在fivethyeye.com的圖表中,它們主要在軸的頂部刻度線上包含y軸標籤。一個例子在本卡斯爾曼的圖形被認爲是從他2016年2月12日的文章「沙特是贏得它的戰爭對美國石油工業」:The number for the top of the y-axis scale includes the unit, like $150/barrel or 1,500 rigs.如何在R中使用ggplot2在軸刻度標記上添加軸標籤,類似於fivethirtyeight樣式?

我已經修改從ggthemes包一個主題GGPLOT2:

library(ggplot2) 
library(ggthemes) 
theme_fivethirtyeight_mod <- function (base_size = 12, base_family = "sans") { 
(theme_foundation(base_size = base_size, base_family = base_family) + 
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"], linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]), 
     axis.text = element_text(color = 'black'), axis.ticks = element_blank(), axis.line = element_blank(), 
     legend.title = element_blank(), legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), 
     legend.position = "bottom", legend.direction = "horizontal", legend.box = "vertical", 
     panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]), 
     panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0, size = rel(1.5), face = "bold"), 
     plot.margin = unit(c(1, 1, 1, 1), "lines"), strip.background = element_rect())) 
} 

我想「後IP」添加到這個曲線圖的y軸和頂部的「IP之前」與x軸的最右邊的刻度(其使用該主題): A scatterplot showing Innings Pitched before an injury versus after an injury for 81 baseball pitchers.

在此先感謝您的幫助!

+0

的[ggthemes包(https://cran.r-project.org/web/packages/ ggthemes/vignettes/ggthemes.html)提供了五十八歲的主題。 – Thomas

+0

但是,ggthemes軟件包不允許使用第五個樣式的軸標籤。另外,fivethirthyeight主題將'axis.text'設置爲'element_blank()',我需要某種形式的標籤。 @Thomas –

回答

0

您可以手動標記刻度:

lab <- c(seq(40, 220, by = 20)) 
p + scale_y_continuous(breaks = seq(40, 240, by = 20), labels = c(lab, "240 IP before injury")) 

這是一個整潔的情節,你打算將其發佈給公衆?我希望看到更多你的分析。

編輯:

使用修改的主題:

library(ggplot2) 
library(ggthemes) 

theme_fivethirtyeight_mod <- function (base_size = 12, base_family = "sans") { 
(theme_foundation(base_size = base_size, base_family = base_family) + 
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"], linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]), 
     axis.text = element_text(color = 'black'), axis.ticks = element_blank(), axis.line = element_blank(), 
     legend.title = element_blank(), legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), 
     legend.position = "bottom", legend.direction = "horizontal", legend.box = "vertical", 
     panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]), 
     panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0, size = rel(1.5), face = "bold"), 
     plot.margin = unit(c(1, 1, 1, 1), "lines"), strip.background = element_rect())) 
} 

p1 <- ggplot(ChickWeight, aes(Time, weight)) + 
geom_point(position = "jitter") + 
scale_y_continuous(
    expand = c(0, 0), 
    limits = c(0, 560), 
    breaks = seq(0, 560, by = 185), 
    labels = c(seq(0, 375, by = 185), "560 units")) + 
theme_fivethirtyeight_mod() 

Plot with y-axis tick mark at the top.

+0

感謝您的建議,但我已經嘗試過。我正在使用ggtheme的fivethirtyeyeight主題的修改版本,其中我添加了軸標籤的功能。我得到一個警告,我正在取代'y'的比例尺,但我仍然以數字爲單位,沒有文字。我仍然在製作一張符合圖形的文件(以及其他一些文件),但我的代碼和原始數據在線。由於我學習R進行此分析,代碼有點麻煩,但代碼位於https://sites.google.com/site/tpmsca​​pstonecode/。只要我的合作伙伴和我完成,我會在論文中添加論文。 –

+0

我使用修改後的主題再次嘗試了它,它適用於我。看到我上面的編輯。 – foto51

+0

奇妙的工作!謝謝! –