ggplot分组条形图上的对数刻度问题 [英] Trouble with log scale on ggplot grouped bar plot

查看:147
本文介绍了ggplot分组条形图上的对数刻度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的ggplot2制作对数刻度的成组条形图.我的目标是在R中重新创建以下图.

I'm trying to make a grouped bar plot with a logarithmic scale using ggplot2 in R. My goal was to recreate the following plot in R.

因为产生它的程序无法制作高分辨率图.我需要一个对数刻度,因为数字范围从1到超过1000,且介于两者之间.

Because the program that produced it cannot make high-resolution graphs. I need a log scale because the numbers range from 1 to over 1000, and everywhere in between.

这是数据框的简化版本以及我一直在使用的代码的摘要.我已经能够使用ggplot2进行绘图,但是我的问题是我在数据中有很多1,最终被绘制为0,而0则显示为-1.这是我的R图.

This is a snippet of a simplified version of the dataframe, as well as the code I've been using. I have been able to make the plot using ggplot2, but my issue is that I have a lot of 1s in the data that end up being plotted as 0s, and 0s that show up as -1. Here is what my R plot looks like.

genus_counts <- read.table(text = "Genus variable value
1  Lepisosteus  JBGC462     0
2      Lepomis  JBGC462     6
3  Micropterus  JBGC462     2
4        Perca  JBGC462     2
5    Ictalurus  JBGC462     1
6  Lepisosteus   JBGC13    13
7      Lepomis   JBGC13     0
8  Micropterus   JBGC13     0
9        Perca   JBGC13     0
10   Ictalurus   JBGC13     0", header = TRUE)


ggplot(genus_counts, aes(x=Genus, y=value, fill=variable))+
      geom_bar(stat="identity", position="dodge")+
      scale_y_log10()

从数学上来说,我理解为什么会这样(而且条形图上的对数刻度并不是很理想).但是,还有另一种方法可以调整绘图(或输入到绘图中的数字),使其与我要模拟的绘图更接近吗?

Mathematically, I understand why this is the case (and also that log scales on bar plots are not really ideal). But is there another way I can adjust the plot (or the numbers I'm feeding into the plot) to get a closer match to the plot I'm trying to emulate?

推荐答案

如果您使用scale_y_sqrt(),则看起来像这样,这似乎与您的示例图非常匹配.我添加了一个值为1000的行,以说明可以同时看到1和2这样的小值,以及大的值.

It looks like this if you use scale_y_sqrt() instead, which seems to be a pretty good match for your example plot. I added a row with a value of 1000 to illustrate that you can see both small values like 1 and 2, along side the large ones.

这篇关于ggplot分组条形图上的对数刻度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆