[英]How to choose the right parameters for dotplot in r ggplot
I intend to make a dot plot somewhat like this: 我打算制作一个像这样的点图:
But there's some issue with the code: 但是代码存在一些问题:
df = data.frame(x=runif(100))
df %>%
ggplot(aes(x )) +
geom_dotplot(binwidth =0.01, aes(fill = ..count..), stackdir = "centerwhole",dotsize=2, stackgroups = T, binpositions = "all")
how to choose bin width to avoid dots overlapping, bins wrapping itself in 2 columns or dots get truncated at the top and bottom? 如何选择纸箱宽度以避免点重叠,将纸箱自身包裹成两列或在顶部和底部截断点?
And why is the y axis showing decimal points instead of count? 为什么y轴显示小数点而不是计数? And how to color the dots by x value?
以及如何通过x值给点着色? I tried fill = x and no color is shown.
我尝试了fill = x,但没有显示颜色。
First from the help of ?geom_dotplot
首先从
?geom_dotplot
的帮助
When binning along the x axis and stacking along the y axis, the numbers on y axis are not meaningful, due to technical limitations of ggplot2.
沿x轴进行装仓并沿y轴堆叠时,由于ggplot2的技术限制,y轴上的数字没有意义。 You can hide the y axis, as in one of the examples, or manually scale it to match the number of dots.
您可以隐藏y轴(如上述示例之一),也可以手动缩放它以匹配点数。
Thus you can try following. 因此,您可以尝试关注。 Note, the coloring is not completly fitting the x axis.
注意,着色不能完全适合x轴。
library(tidyverse)
df %>%
ggplot(aes(x)) +
geom_dotplot(stackdir = "down",dotsize=0.8,
fill = colorRampPalette(c("blue", "white", "red"))(100)) +
scale_y_continuous(labels = c(0,10), breaks = c(0,-0.4)) +
scale_x_continuous(position = "top") +
theme_classic()
For the correct coloring, you have to calculate the bins by yourself using eg .bincode
: 为了正确着色,您必须使用
.bincode
自己计算垃圾箱:
df %>%
mutate(gr=with(.,.bincode(x ,breaks = seq(0,1,1/30)))) %>%
mutate(gr2=factor(gr,levels = 1:30, labels = colorRampPalette(c("blue", "white", "red"))(30))) %>%
arrange(x) %>%
{ggplot(data=.,aes(x)) +
geom_dotplot(stackdir = "down",dotsize=0.8,
fill = .$gr2) +
scale_y_continuous(labels = c(0,10), breaks = c(0,-0.4)) +
scale_x_continuous(position = "top") +
theme_classic()}
The overlap is caused by the dotsize
> 1; 重叠是由点
dotsize
> 1引起的; as @Jimbuo said, the decimal values on the y axis is due to the internals of this geom; 如@Jimbuo所说,y轴上的十进制值是由于此几何的内部而引起的; for the
fill
and color
you can use the ..x..
computed variable: 对于
fill
和color
,可以使用..x..
计算变量:
Computed variables
计算变量
x center of each bin, if binaxis is "x"
每个bin的x中心(如果binaxis为“ x”)
df = data.frame(x=runif(1000))
library(dplyr)
library(ggplot2)
df %>%
ggplot(aes(x, fill = ..x.., color = ..x..)) +
geom_dotplot(method = 'histodot',
binwidth = 0.01,
stackdir = "down",
stackgroups = T,
binpositions = "all") +
scale_fill_gradientn('', colours = c('#5185FB', '#9BCFFD', '#DFDFDF', '#FF0000'), labels = c(0, 1), breaks = c(0,1), guide = guide_legend('')) +
scale_color_gradientn(colours = c('#5185FB', '#9BCFFD', '#DFDFDF', '#FF0000'), labels = c(0, 1), breaks = c(0,1), guide = guide_legend('')) +
scale_y_continuous() +
scale_x_continuous('', position = 'top') +
# coord_equal(ratio = .25) +
theme_classic() +
theme(axis.line = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
aspect.ratio = .25,
legend.position = 'bottom',
legend.direction = 'vertical'
)
Created on 2018-05-18 by the reprex package (v0.2.0). 由reprex软件包 (v0.2.0)于2018-05-18创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.