我想在填充条形图上添加百分比数字。这是标签位于错误位置的图:
这是数据框:
x0 <- expand.grid(grp = c("G1","G2")
, treat = c("T1","T2")
, out = c("out1","out2","out3","out4")
)
set.seed(1234)
x0$n <- round(runif(16,0,1)*100,0)
head(x0)
grp treat out n
1 G1 T1 out1 11
2 G2 T1 out1 62
3 G1 T2 out1 61
4 G2 T2 out1 62
5 G1 T1 out2 86
6 G2 T1 out2 64
Run Code Online (Sandbox Code Playgroud)
现在,我将 grp/treat 中的总和添加到数据帧中(使用 sql,抱歉!):
x0 <- sqldf(paste("SELECT a.*, (SELECT SUM(n)"
," FROM x0 b"
," WHERE a.grp = b.grp"
," AND a.treat = …Run Code Online (Sandbox Code Playgroud) 是否可以使用存储在列名数组中的列名对数据框列进行子集化(到新的 df 中) - 就像 c("col1", "col9", "col6") 中那样?我知道我可以使用 df[[colname]] 语法引用 df 中的一列,但它不允许我对多列执行此操作:
df
X1 X2 X3
1: a 1 3
2: b 5 3
3: a 3 4
4: c 6 5
5: c 2 2
cnm<-c("X2","X3")
df[[cnm]]
Run Code Online (Sandbox Code Playgroud)
.subset2(x,i,exact =exact)中的错误:下标超出范围
谢谢
我有这个点图:
ggplot(mpg, aes(drv, hwy)) +
geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center')
Run Code Online (Sandbox Code Playgroud)
呈现为
我想按制造商为点着色。如果我添加fill美学:
ggplot(mpg, aes(drv, hwy, fill = manufacturer)) +
geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center')
Run Code Online (Sandbox Code Playgroud)
它呈现为
看起来添加颜色以某种方式击败了堆叠算法,导致点重叠。我该如何解决?
使用Wickhams中的示例介绍数据科学中的purrr,我试图创建一个双嵌套列表.
library(gapminder)
library(purrr)
library(tidyr)
gapminder
nest_data <- gapminder %>% group_by(continent) %>% nest(.key = by_continent)
Run Code Online (Sandbox Code Playgroud)
如何进一步嵌套国家/地区,以便nest_data包含by_continent和最新包含by_conar的嵌套by_contry,最终包含t_bled?
此外,在为gapminder数据创建此数据结构后 - 您将如何运行每个国家/地区的bookchapter中的回归模型示例?
下面是一个图表。如何反转 Y 轴,使其向下读取“a b”而不是“b a”,同时单独保留 X 轴?
代码:
library(ggplot2)
levels = ordered(c('a', 'b'))
data = data.frame(x=ordered(c('a', 'a', 'b', 'b'), levels=levels),
y=ordered(c('a', 'b', 'a', 'b'), levels=levels),
prob=c(0.3, 0.7, 0.4, 0.6))
ggplot(data, aes(x, y)) + geom_tile(aes(fill=prob))
Run Code Online (Sandbox Code Playgroud) 我可以使用dplyr :: select(ends_with)选择适合多种条件的列名称。考虑到我的列名,我想使用结尾于而不是包含或匹配项,因为我要选择的字符串在列名的末尾相关,但也可能出现在其他中间。例如,
df <- data.frame(a10 = 1:4,
a11 = 5:8,
a20 = 1:4,
a12 = 5:8)
Run Code Online (Sandbox Code Playgroud)
我想选择以1或2结尾的列,以仅具有a11和a12列。select(ends_with)是最好的方法吗?
谢谢!
我有一个具有以下结构的数据帧“ pd.long”:
> str(pd.long)
'data.frame': 144 obs. of 3 variables:
$ Site : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ...
$ Month : Factor w/ 12 levels "Dec","Jan","Feb",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Density: int 20 20 20 20 20 20 20 20 20 20 ...
Run Code Online (Sandbox Code Playgroud)
我用这段代码构建了条形图:
ggplot(pd.long, aes(x=Month, y=Density, fill=Site))+
geom_bar(stat = "identity", position = "dodge", fill = "darkgray")+
xlab("Month") + ylab("Number")+
facet_wrap(~Site, …Run Code Online (Sandbox Code Playgroud) 我在图的顶部有一个图例。我希望图例保持左对齐,并能够设置美学符号(彩色正方形)和文本之间的间距(1),以及文本和下一个美学符号之间的间距(2)。
library(tidyverse)
mtcars %>%
mutate(transmission = ifelse(am, "manual", "automatic")) %>%
ggplot() +
aes(x = transmission, fill = transmission) +
geom_bar() +
labs(fill = NULL) +
theme(
#legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
legend.position = "top",
legend.justification = c(0,0),
legend.title=element_blank(),
legend.margin=margin(c(5,5,5,0)))
Run Code Online (Sandbox Code Playgroud)
我正在尝试将数据(见下文)绘制为分组条形图.我已经按照其他建议改变position = ""了geom_bar,但我似乎无法使其发挥作用.我得到的只是一个堆积条形图.这是我最近的尝试:
ggplot(data = IFNg, aes(x = IFNg$Location, y = IFNg$`Fold Change`)) +
geom_bar(position = 'identity', stat = "identity", fill = IFNg$Day) +
ylab("Fold Change (log_2)") + geom_errorbar(limits, width=0.15)
Run Code Online (Sandbox Code Playgroud)
我也试过以下也会产生叠加图,但是这个图的颜色不同:
ggplot(data = IFNg, aes(x = IFNg$Location, y = IFNg$`Fold Change`,
fill= IFNg$Day)) + geom_bar(position = 'identity', stat = "identity") +
ylab("Fold Change (log_2)") + geom_errorbar(limits, width=0.15)
Run Code Online (Sandbox Code Playgroud)
这是我的其余代码与数据:
dat <- structure(list(Day = c(1L, 3L, 7L, 21L, 1L, 3L, 7L, 21L, 1L,
3L, 7L, 21L), Order = …Run Code Online (Sandbox Code Playgroud) 我正在使用 pheatmap 包。默认情况下,它将绘图绘制到屏幕上。就我而言,这意味着在 R studio 的 R markdown notebook 中输出。但我也想保存到文件中。如果我将它保存到一个文件中,并为其提供filename=参数,它就不会绘制到屏幕上(R 笔记本)。有没有办法让这两件事都发生?更一般地说,对于我想在屏幕上保存和显示的任何情节(ggplot2)?