如何使用 ggplot2 将 y 轴重新居中于 0.01
How do you recenter y-axis at 0.01 using ggplot2
我正在尝试制作一个分组条形图,比较来自 2 个不同位置站点比较图的污染物浓度(在 ug/g 中),y 轴为对数刻度。我有一些小于一但大于零的值。 y 轴将我的条形居中于 y=1,使一些条形看起来是负的。是否可以让我的条从 0.01 而不是 1 开始?
我试过 coord_cartesian( ylim=c(0.01,100), expand = FALSE )
但没有成功。我还尝试使用 coor_trans(y='log10')
而不是 scale_y_continuous(trans='log10')
来记录变换 y 轴,但收到错误消息“变换在 y 轴中引入了无限值”,即使我没有零值。
如有任何帮助,我们将不胜感激,
谢谢。
我的代码如下:
malcomp2 %>%
ggplot(aes(x= contam, y= ug_values, fill= Location))+
geom_col(data= malcomp2,
mapping = aes(x= contam, y= ug_values, fill = Location),
position = position_dodge(.9), #makes the bars grouped
stat_identity(),
colour = "black", #adds black lines around bars
width = 0.8,
size = 0.3)+
ylab(expression(Concentration~(mu*g/g)))+ #adds mu character to y axis
coord_cartesian( ylim=c(0.01,100), expand = FALSE ) + #force bars to start at 0
theme_classic()+ #get rid of grey grid
scale_y_continuous(trans='log10', #change to log scale
labels = c(0.01,0.1,1,10,100))+ #change axis to not sci notation
annotation_logticks(sides = 'l')+ #add log ticks to y axis only
scale_x_discrete(name = NULL, #no x axis label
limits = c('Dieldrin','Mirex','PBDEs','CHLDs','DDTs','PCBtri','PCBquad',
'PCBhept'), #changes order of x axis
labels = c('Dieldrin','Mirex','PBDEs','CHLDs','DDTs','PCB 3','PCB 4-6','PCB7+'))+
scale_fill_manual("Location", #rename legend
values = c('turquoise2','gold'), #change colors
labels = c( 'St.Andrew Bay', 'Sapelo'))+ #change names on legend
theme(legend.title = NULL,
legend.key.size = unit(15, "pt"),
legend.position = c(0.10,0.95)) #places legend in upper left corner
一种技巧是只缩放数据,使基线为 1。人们之前在 SO 上问过这个问题,似乎更令人满意的方法可能是使用 geom_rect
代替,就像这里:
Setting where y-axis bisects when using log scale in ggplot2 geom_bar
ggplot(data.frame(contam = 1:5, ug_values = 10^(-2:2)*100),
aes(contam, ug_values)) +
geom_col() +
scale_y_continuous(trans = 'log10', limits = c(1,10000),
breaks = c(1,10,100,1000,10000),
labels = c(0.01,0.1,1,10,100))
我正在尝试制作一个分组条形图,比较来自 2 个不同位置站点比较图的污染物浓度(在 ug/g 中),y 轴为对数刻度。我有一些小于一但大于零的值。 y 轴将我的条形居中于 y=1,使一些条形看起来是负的。是否可以让我的条从 0.01 而不是 1 开始?
我试过 coord_cartesian( ylim=c(0.01,100), expand = FALSE )
但没有成功。我还尝试使用 coor_trans(y='log10')
而不是 scale_y_continuous(trans='log10')
来记录变换 y 轴,但收到错误消息“变换在 y 轴中引入了无限值”,即使我没有零值。
如有任何帮助,我们将不胜感激, 谢谢。
我的代码如下:
malcomp2 %>%
ggplot(aes(x= contam, y= ug_values, fill= Location))+
geom_col(data= malcomp2,
mapping = aes(x= contam, y= ug_values, fill = Location),
position = position_dodge(.9), #makes the bars grouped
stat_identity(),
colour = "black", #adds black lines around bars
width = 0.8,
size = 0.3)+
ylab(expression(Concentration~(mu*g/g)))+ #adds mu character to y axis
coord_cartesian( ylim=c(0.01,100), expand = FALSE ) + #force bars to start at 0
theme_classic()+ #get rid of grey grid
scale_y_continuous(trans='log10', #change to log scale
labels = c(0.01,0.1,1,10,100))+ #change axis to not sci notation
annotation_logticks(sides = 'l')+ #add log ticks to y axis only
scale_x_discrete(name = NULL, #no x axis label
limits = c('Dieldrin','Mirex','PBDEs','CHLDs','DDTs','PCBtri','PCBquad',
'PCBhept'), #changes order of x axis
labels = c('Dieldrin','Mirex','PBDEs','CHLDs','DDTs','PCB 3','PCB 4-6','PCB7+'))+
scale_fill_manual("Location", #rename legend
values = c('turquoise2','gold'), #change colors
labels = c( 'St.Andrew Bay', 'Sapelo'))+ #change names on legend
theme(legend.title = NULL,
legend.key.size = unit(15, "pt"),
legend.position = c(0.10,0.95)) #places legend in upper left corner
一种技巧是只缩放数据,使基线为 1。人们之前在 SO 上问过这个问题,似乎更令人满意的方法可能是使用 geom_rect
代替,就像这里:
Setting where y-axis bisects when using log scale in ggplot2 geom_bar
ggplot(data.frame(contam = 1:5, ug_values = 10^(-2:2)*100),
aes(contam, ug_values)) +
geom_col() +
scale_y_continuous(trans = 'log10', limits = c(1,10000),
breaks = c(1,10,100,1000,10000),
labels = c(0.01,0.1,1,10,100))