ggplot2点图中无意义的Y轴值

时间:2016-02-05 05:23:32

标签: r ggplot2 axis

我正在尝试复制Fivethirtyeight's Tarantino movie plot。这个图是使用ggplot2的dotplot。可以找到数据here

我的代码如下:

tara <- read.csv("tarantino.csv")
dim(tara)
names(tara)
table(tara$movie)

rd <- subset(tara, movie=="Reservoir Dogs")
du <- subset(tara, movie=="Django Unchained")
ib <- subset(tara, movie=="Inglorious Basterds") 
jb <- subset(tara, movie=="Jackie Brown")
kb1 <- subset(tara, movie=="Kill Bill: Vol. 1")
kb2 <- subset(tara, movie=="Kill Bill: Vol. 2")
pf <- subset(tara, movie=="Pulp Fiction")



plot1 <- ggplot(rd, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() + theme(legend.position="none") +
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  ggtitle(" RESERVIOR DOGS") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")
plot2 <- ggplot(pf, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" PULP FICTION") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")
plot3 <- ggplot(du, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" DJANGO UNCHAINED") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot4 <- ggplot(ib, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" INGLORIOUS BASTARDS") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot5 <- ggplot(jb, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" JACKIE BROWN") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot6 <- ggplot(kb1, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="top")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  theme(legend.position="none")  +
  ggtitle(" KILL BILL: VOL 1") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("")

plot7 <- ggplot(kb2, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth=0.5, method="histodot") +
  theme_bw() +theme(legend.position="none")+
  ylim(0,20)+
  # Set the entire chart region to a light gray color
  theme(panel.background=element_rect(fill="#F0F0F0")) +
  theme(plot.background=element_rect(fill="#F0F0F0")) +
  theme(panel.border=element_rect(colour="#F0F0F0")) +
  ggtitle(" KILL BILL: VOL 2") +
  theme(plot.title=element_text(face="bold",hjust=-.08,vjust=2,colour="#535353",size=12)) +
  ylab("") +
  xlab("minutes")





library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(7, 1)))
vplayout <- function(x, y)
  viewport(layout.pos.row = x, layout.pos.col = y)
print(plot1, vp = vplayout(1, 1))
print(plot2, vp = vplayout(2, 1))
print(plot3, vp = vplayout(3, 1))
print(plot4, vp = vplayout(4, 1))
print(plot5, vp = vplayout(5, 1))
print(plot6, vp = vplayout(6, 1))
print(plot7, vp = vplayout(7, 1))

输出如下:

enter image description here

情节有几个问题:

  • Y轴无法控制。我可以使用coord_fixed(ratio=X);但它没有多大帮助。
  • 点的大小不同。我试过用dotsize;但它不起作用。
  • 无法在背景中保留背景grey

1 个答案:

答案 0 :(得分:3)

这是一种方法:

# download data
download.file('https://raw.githubusercontent.com/fivethirtyeight/data/master/tarantino/tarantino.csv', '~/Desktop/tarantino.csv', method = 'curl')

# read in data
tara <- read.csv('~/Desktop/tarantino.csv')

library(ggplot2)
library(ggthemes)     # has theme_fivethirtyeight, which will save a lot of work

ggplot(tara, aes(x=minutes_in, fill=type)) +
  geom_dotplot(binwidth = 1, method = "histodot") +
  theme_fivethirtyeight() + 
  ylim(0, 20) +
  ggtitle('Deaths and Swearing in Tarantino Films') +
  theme(strip.text = element_text(hjust = 0)) +     # left justify facet titles
  ylab("") +
  xlab("") + 
  # facet_wrap defaults to titles on top. The labeller capitalizes movie titles.
  facet_wrap(~movie, ncol = 1, labeller = labeller(movie = toupper))

# bin width/dot size is CRAZY SENSITIVE to aspect ratio. width 4:height 5:binwidth 1 lines up with your y-axis
ggsave('~/Desktop/Rplot.png', width = 8, height = 10, units = 'in')

你得到了什么:

dotplot of Tarantino films