[英]stacked histogram changes scale when filled in R
I created a stacked histogram with a data of this nature:我用这种性质的数据创建了一个堆叠直方图:
> haps
tot K7a
1 1 2
2 7 1
3 1 4
4 8 3
5 1 6
tot is counts of observations of each element, (I am showing only 5 elements of the 1899 in the table above), K7a is a factor with 7 levels. tot 是每个元素的观察计数,(我在上表中只显示了 1899 中的 5 个元素),K7a 是一个具有 7 个级别的因子。 The data should be read eg as: element 1 is observed once and belongs to group 2, ... element 4 is observed 8 times and belongs to group 3, etc.
数据应该读作例如:元素 1 被观察一次,属于第 2 组,...元素 4 被观察 8 次,属于第 3 组,等等。
with the following code I get a histogram in B&W:使用以下代码,我得到了黑白直方图:
ggplot(haps, aes(x=tot) +
geom_histogram(binwidth = 0.5) +
scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000, 1500, 2000)) +
scale_x_continuous(limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" )
Then I want to add colour to the 7 groups, and I use the following code, simply adding fill to aes:然后我想给7组加颜色,我用下面的代码,简单的给aes加了fill:
ggplot(haps, aes(x=tot, fill=K7a)) +
geom_histogram(binwidth = 0.5) +
scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000, 1500, 2000)) +
(limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" )
the second graph changes the y scale even though I use the same code, I can't figure out how to get a graph with the same scale as in the black & white one.即使我使用相同的代码,第二张图也会更改 y 比例,但我无法弄清楚如何获得与黑白图相同比例的图。
Not really sure what's happening.不太确定发生了什么。 My best guess is the transformation is applied only to the
y
scale and not to the fill
scale.我最好的猜测是变换仅适用于
y
比例而不适用于fill
比例。
You can 'work around' this issue applying the transformation after all the calculations have been made using coord_trans()
:在使用
coord_trans()
进行所有计算后,您可以“解决”应用转换的这个问题:
library(ggplot2)
df <- read.table(text="
tot K7a
1 1 2
2 7 1
3 1 4
4 8 3
5 1 6")
ggplot(df, aes(tot, fill = factor(K7a))) +
scale_y_continuous( breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000,1500, 2000)) +
geom_histogram(binwidth = 1) +
coord_trans(y = 'log1p')
Created on 2020-10-18 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 10 月 18 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.