我想在我的骑行图中画一条线来表示平均值。内置的分位数参数以我想要的样式画一条线,但在中位数. 我怎样才能平均绘制一个,最好不使用geom_vline()或ggplot 构建对象的 pluck但留在 ggridges 生态中?
library(tidyverse)
library(ggridges)
#adding a column for the mean of each Species of the iris dataframe
iris_meaned <- iris %>%
group_by(Species) %>%
mutate(mean_petal_len = mean(Petal.Length)) %>%
ungroup()
iris_meaned %>%
ggplot() +
geom_density_ridges(
aes(x = Petal.Length, y = Species, fill = Species),
quantile_lines = T, quantiles = 2 #adding lines for the median
) +
geom_text(
aes(x = mean_petal_len, y = Species, label = round(mean_petal_len, 2)),
size = 2, nudge_x …Run Code Online (Sandbox Code Playgroud) 包中的geom_density_ridgesgeom ggridges创建了脊线,如果没有指定带宽,它会尝试找到合理的值.然后使用baseR message函数报告该值(请参阅https://twitter.com/ClausWilke/status/921363157553172480).
的base一个R函数suppressMessages功能被设计为抑制这样的消息.例如,此代码输出一条消息:
message('This is a message');
Run Code Online (Sandbox Code Playgroud)
这段代码什么都没输出:
suppressMessages(message('This is a message'));
Run Code Online (Sandbox Code Playgroud)
但是,出于某种原因,当将这个geom添加到ggplot时,抑制消息似乎被抑制了.下面的代码也仍然会产生一条消息:
require('ggplot2');
require('ggridges');
suppressMessages(ggplot(Orange, aes(x=age,y=Tree)) + geom_density_ridges());
Run Code Online (Sandbox Code Playgroud)
(具体来说," Picking joint bandwidth of 319".)
为什么是这样?ggplot无论用户的规格如何,是否确实能够确保消息传递?或者这实际上是我刚才不知道的明智行为?
生成RMarkdown报告时,message可以将chunk选项设置为message=FALSE,这将抑制呈现级别的所有消息.因为那是我的用例,我的问题就解决了.
正如该ggridges软件包的作者Claus Wilke 建议的那样,您可以随时bandwidth手动设置以避免消息(https://twitter.com/ClausWilke/status/921361195231215616).
但为什么不suppressMessages首先压制消息呢?
我恰好不知道这种预期的行为吗?
当传奇标题很长时,我很难将传奇中心的标题相对于图例键对齐.一年前有一个问题适用于短标题,但它似乎不适用于长标题.
示例,首先使用简短的图例标题:
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "A") +
theme(legend.title.align = 0.5)
Run Code Online (Sandbox Code Playgroud)
一切都如预期的那样,图例标题位于图例键的上方.
现在和一个长长的传奇标题相同:
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "Long legend heading\nShould be centered") +
theme(legend.title.align = 0.5)
Run Code Online (Sandbox Code Playgroud)
我们可以看到文本中心与自身对齐,但与图例键无关.我已经尝试修改其他主题选项,例如legend.justification = "center",但似乎没有人从图例框中最左侧的位置移动键.
几条评论:
我几天前正在运行ggplot2的开发版本,v2.2.1.9000.
我特别需要一个连续色阶调色板的解决方案.
我有以下数据集(示例):
train <- data.frame(ps_ind_06_bin = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE),
ps_ind_07_bin = c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE),
ps_ind_08_bin = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE),
ps_ind_09_log = c(1, 3, 4, 2, 3, 2))
Run Code Online (Sandbox Code Playgroud)
我有以下函数显示group_by()操作的ggplot :
get_charts1 <- function(mygroup){
quo_var <- enquo(mygroup)
train %>%
group_by(!!quo_var) %>%
count() %>%
ungroup() %>%
ggplot(aes_q(x = quo_var, y = quote(n), fill = quo_var)) +
geom_col() +
theme(legend.position = "none")
}
Run Code Online (Sandbox Code Playgroud)
我手动输入列名称时工作正常,例如:
get_charts1(ps_ind_07_bin)
Run Code Online (Sandbox Code Playgroud)
但是,我想在几个列上使用该函数,我将它放在一个向量上:
binarias <- train %>%
select(ends_with("bin")) %>%
colnames()
Run Code Online (Sandbox Code Playgroud)
使用地图并提出一些建议,我试图使用: …
关于这个主题显然有很多问题,但我看不出任何一般的解决方案:我有一个深度递归的列表,并希望将其展平为包含所有非列表项的单个列表.
例如,采用此嵌套列表:
d = list(
list(
list(
iris[sample(1:150,3),],
iris[sample(1:150,3),]
),
list(
list(
iris[sample(1:150,3),],
list(
iris[sample(1:150,3),],
iris[sample(1:150,3),]
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
把它变成这个:
list(iris[sample(1:150,3),],
iris[sample(1:150,3),],
iris[sample(1:150,3),],
iris[sample(1:150,3),],
iris[sample(1:150,3),])
Run Code Online (Sandbox Code Playgroud)
我根据其他解决方案尝试了以下一些方法:
purrr::flatten(d)
plyr::llply(d, unlist)
lapply(d, unlist, use.names=FALSE)
Run Code Online (Sandbox Code Playgroud)
没有达到期望的结果,在该示例中是单个列表长度5,所有项目是a data.frame.任何建议赞赏.
我正在开发一个R包,需要使用snowfall包装提供的并行化.snowfall似乎没有导入同为其他包一样ggplot2,data.table等我已经包括snowfall,rlecuyer以及snow在描述文件,命名空间的文件,并在函数本身的进口参数.当我尝试访问此功能时,我收到以下错误:
Error in sfInit() : could not find function "setDefaultClusterOptions"
该sfInit函数似乎有一个nostart/ nostop参数,它表示与嵌套用法有关,sfInit但这对我来说似乎也没有.
实际的代码本身使用一个sfInit(我得到错误的地方),一些sfExports和sfLibrarys,以及一个sfLapply.
可能的解决方案:如果我snow从导入部分移动到Desciption文件中的depends部分,它似乎有效.我不知道为什么.
假设我有一个像这样的多面 ggplot:
data(iris)
ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
facet_grid(. ~ Species) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
题:
是否可以仅将第一个方面标签(“setosa”)与左侧对齐,但将其他方面保持在中央?
我对直接没有任何运气ggplot,所以我尝试转换为 gtable(下面的代码),我可以看到我可能需要修改的元素是 tableGrb #13 - strip-t-1,但我不确定怎么做。我尝试将gt$layout$l和gt$layout$r值更改为 4(原为 5),但这会将标题完全移出构面。
pl <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
facet_grid(. ~ Species) +
geom_point()
gt <- ggplotGrob(pl)
gt$layout[gt$layout$name == 'strip-t-1', c('l', 'r')] <- c(4, 4) # my attempt
grid.newpage()
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)
有谁知道这是否可能?
当我使用 ggplot2 从 R 保存图形时,geom_col()或者geom_bar(stat = 'identity')它会出现一些难看的水平白线。
数据如下:
> head(mouse_track_data)
session site BM.pile ox.pile type mice skinks geckos
1 dec_2018 1 1 1.10 control FALSE TRUE FALSE
2 dec_2018 1 2 1.12 design1 FALSE FALSE FALSE
3 dec_2018 1 3 1.11 control FALSE FALSE FALSE
4 dec_2018 1 4 1.80 control FALSE FALSE FALSE
5 dec_2018 1 5 1.60 design1 FALSE FALSE FALSE
6 dec_2018 1 6 1.50 control FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
ggplot(data = mouse_track_data, …Run Code Online (Sandbox Code Playgroud) 我正在用ggplot2绘制热图。y轴上的几个刻度需要标记。但是,其中一些过于紧密和重叠。我知道ggrepel可以分隔文本标签,但是目前我还没有解决我的问题。
我的代码如下。任何建议都欢迎。谢谢。
码:
df <- data.frame()
for (i in 1:50){
tmp_df <- data.frame(cell=paste0("cell", i),
gene=paste0("gene", 1:100), exp = rnorm(100), ident = i %% 5)
df<-rbind(df, tmp_df)
}
labelRow=rep("", 100)
for (i in c(2, 5, 7, 11, 19, 23)){
labelRow[i] <- paste0("gene", i)
}
library(ggplot2)
heatmap <- ggplot(data = df, mapping = aes(x = cell, y = gene, fill = exp)) +
geom_tile() +
scale_fill_gradient2(name = "Expression") +
scale_y_discrete(position = "right", labels = labelRow) +
facet_grid(facets = ~ident,
drop = TRUE, …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种有效的方法将数据映射到ggplot2中的图例文本颜色,就像我们可以使用轴文本一样.可重复的例子如下.
首先,让我们制作一个情节:
library(ggplot2)
library(dplyr)
drv_counts <- mutate(mpg,
drv = case_when(drv == "r" ~ "rear wheel drive",
drv == "4" ~ "4 wheel drive",
drv == "f" ~ "front wheel drive"),
model_drv = interaction(model, drv)) %>%
group_by(model_drv) %>%
summarize(model = model[1], drv = drv[1], count = n()) %>%
arrange(drv, count) %>%
mutate(model = factor(model, levels = model))
p <- ggplot(drv_counts, aes(x=model, y=count, fill=drv)) +
geom_col() + coord_flip() + guides(fill = guide_legend(reverse=T)) +
theme_minimal()
p
Run Code Online (Sandbox Code Playgroud)
现在让我们通过传动系为轴标签着色.这很容易:
# ggplot2 colors
cols <- c("4 …Run Code Online (Sandbox Code Playgroud)