如何在R中使用ggplot2在轴刻度标记上添加轴标签,类似于五十六种样式?

时间:2016-04-16 03:33:16

标签: r ggplot2

在fivethirtyeight.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.

中的Ben Casselman图表中看到了一个例子。

我已经从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()))
}

我想添加&#39; IP After&#39;到此图表的y轴顶部和&#39; IP之前&#39;到x轴最右边的刻度线(使用该主题): A scatterplot showing Innings Pitched before an injury versus after an injury for 81 baseball pitchers.

提前感谢所有帮助!

1 个答案:

答案 0 :(得分:1)

您可以手动标记刻度:

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.