当我在R中使用ggplot2和
数据如下:
geom_col()
或geom_bar(stat = 'identity')
保存图形时,图形会出现一些不美观的水平白线。数据如下:
> head(mouse_track_data)
session site BM.pile ox.pile type mice skinks geckos
1 dec_2018 1 1 1.10 control FALSE TRUE FALSE
2 dec_2018 1 2 1.12 design1 FALSE FALSE FALSE
3 dec_2018 1 3 1.11 control FALSE FALSE FALSE
4 dec_2018 1 4 1.80 control FALSE FALSE FALSE
5 dec_2018 1 5 1.60 design1 FALSE FALSE FALSE
6 dec_2018 1 6 1.50 control FALSE FALSE FALSE
这是我的代码:
ggplot(data = mouse_track_data, aes(x = session, y = as.numeric(mice), fill = type)) +
geom_bar(stat='identity') +
theme_classic() +
theme(text = element_text(size=15),
axis.text.x = element_text(angle = 45, hjust = 1)) +
xlab("Tracking session") +
ylab("Number of tunnels with mouse tracks") +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 7, b = 0, l = 0))) +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 7, b = 0, l = 0))) +
theme(legend.title = element_blank(), legend.position = c(-2, -2)) +
scale_fill_manual(values = c("control" = "#37a1c1", "design1" = "#9ce2cb", "design2" = "#445a9c"),
labels = c(" Control", " Single grade", " Mixed grade")) +
scale_x_discrete(labels = c('Dec 2019', "April 2019", "Nov 2019", "March 2020")) +
scale_y_continuous(breaks=seq(0, 50, 10), limits = c(0,52), expand = c(0,0)) +
theme(axis.line = element_blank())
使用此方法或使用geom_col()
时,当导出为ESP或PDF格式时,我得到的图表如下:
每列都有细白线。我猜这与它计算每个数据点然后将它们堆叠有关?
如果我使用geom_bar()
进行编码,则没有白线,但它不能按照我的想法显示数据。