为了绘制半密度,我使用了这篇文章中描述的功能:用ggplot2分割小提琴图
然而,当我想借鉴密度的位数,就像一个正常的geom_violin()或geom_boxplot(),我得到一个错误信息.
我也有兴趣增加每半密度以上的观测数量.
这是我想要获得的一个例子:
data("diamonds")
library(ggplot2)
# Function described in a previous post
GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin, draw_group = function(self, data, ..., draw_quantiles = NULL){
data <- transform(data, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x))
grp <- data[1,'group']
newdata <- plyr::arrange(transform(data, x = if(grp%%2==1) xminv else xmaxv), if(grp%%2==1) y else -y)
newdata <- rbind(newdata[1, ], newdata, newdata[nrow(newdata), ], newdata[1, ])
newdata[c(1,nrow(newdata)-1,nrow(newdata)), 'x'] <- round(newdata[1, …Run Code Online (Sandbox Code Playgroud) 我绘制了与离散变量的类别相对应的几个值密度。我可以同时为每个密度关联特定颜色或颜色渐变。现在我想为每个具有不同值的密度添加一个特定的渐变。
这是一个可重现的示例,使用ggridges:
data(iris)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
cols <- brewer.pal(3, "BrBG")
# Plot with one color per group
ggplot(iris, aes(Sepal.Length, as.factor(Species))) +
geom_density_ridges(aes(fill = as.factor(Species))) +
scale_fill_manual("Sepal", values = cols)
Run Code Online (Sandbox Code Playgroud)
# Plot with one gradient
ggplot(iris, aes(Sepal.Length, as.factor(Species))) +
geom_density_ridges_gradient(aes(fill = ..x..)) +
scale_fill_gradient2(low = "grey", high = cols[1], midpoint = 5)
Run Code Online (Sandbox Code Playgroud)
我基本上想结合两个情节。我也对midpoint每个密度有一个特定的值感兴趣。
我有一个向量,其中重复某些条目。从这个向量中,我想获得所有可能且独特的排列。
环顾四周,我看到了gtools::permutations()推荐了几次,但它不能解决我的问题。
我找到了一种方法RcppAlgos::permuteGeneral()但问题是它将每个条目视为唯一值,然后我必须在第二步中删除重复项。这可能会导致内存问题。
有没有一种简单快速的方法来从向量中获取所有独特的排列?
这是一个可重现的示例:
library(RcppAlgos)
ex <- c("sp1", "sp2", "sp2") # sp2 is repeated twice
perm <- permuteGeneral(v = ex, m = length(ex), repetition = FALSE, freqs = NULL)
perm <- as.data.frame(perm)
perm # some rows are identical (rows 1&2; 3&5, 4&6)
V1 V2 V3
1 sp1 sp2 sp2
2 sp1 sp2 sp2
3 sp2 sp1 sp2
4 sp2 sp2 sp1
5 sp2 sp1 sp2
6 sp2 sp2 sp1
perm[!duplicated(perm), ] # this is what …Run Code Online (Sandbox Code Playgroud) 为了增加具有许多变量的对图的可读性,我想对函数提供的相关系数进行舍入ggpairs()。
在下面的示例中,我希望显示 0.8,而不是显示系数 0.807。
library(GGally)
data(cars)
ggpairs(cars,
upper = list(continuous = wrap(ggally_cor, alignPercent = 0.8, size = 10)))
Run Code Online (Sandbox Code Playgroud)