我想保存以下情节:
library(ggplot2)
myplot <- ggplot(iris) +
geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
ylab(expression(~delta^13*C~"[\211]"))
ggsave(myplot,
filename = "myplot.png",
type = "cairo")
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
Metric information not available for this family/device
Run Code Online (Sandbox Code Playgroud)
问题一定是表达式和 unicode 的组合,因为这两者可以工作:
myplot <- ggplot(iris) +
geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
ylab("[\211]")
myplot <- ggplot(iris) +
geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
ylab(expression(~delta^13*C))
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
编辑:我想打印“千分之一”符号。显然它的unicode是U2030,我错误地认为“\211”是一个unicode,但一定是别的东西。
编辑2:与此同时,我发现这个问题也有类似的问题。在那里,一个建议是用 保存情节encoding = "MacRoman",不幸的是这对我不起作用:
Error in png_dev(..., res = …Run Code Online (Sandbox Code Playgroud) 这是一个测试df:
a <- 5:8
b <- c("A", "B", "C", "D")
df <- data.frame(a,b)
Run Code Online (Sandbox Code Playgroud)
我想创建一个条形图并在每个条形图上方添加文字,在顶部下方一定距离,因此我使用y=Inf, vjust=2,但字母现在由它们的顶部而不是字母的底部对齐(即它们不会坐在同一水平线上).有没有办法改变这种情况(不必vjust=2.45为"短"的东西改变某些类似的东西)?
ggplot(df, aes(x=b, y=a)) + geom_bar(stat="identity") +
scale_y_continuous(limits = c(0,9)) +
annotate('text', x=1, y=Inf, vjust=2, label = "a", parse=TRUE) +
annotate('text', x=2, y=Inf, vjust=2, label = "a", parse=TRUE) +
annotate('text', x=3, y=Inf, vjust=2, label = "b", parse=TRUE) +
annotate('text', x=4, y=Inf, vjust=2, label = "b", parse=TRUE)
Run Code Online (Sandbox Code Playgroud)

我想基于其rownames为列表中的每个数据框创建一个列年份.这个问题之前已经被问过,但不幸的是答案没有帮助.那么,有没有办法做到这一点?
mylist <- list(structure(list(a = 1:10), .Names = "a", row.names = 1991:2000, class = "data.frame"),
structure(list(a = 1:10), .Names = "a", row.names = 1992:2001, class = "data.frame"))
Run Code Online (Sandbox Code Playgroud)
预期结果:
[[1]]
a year
1991 1 1991
1992 2 1992
1993 3 1993
1994 4 1994
1995 5 1995
1996 6 1996
1997 7 1997
1998 8 1998
1999 9 1999
2000 10 2000
[[2]]
a year
1992 1 1992
1993 2 1993
1994 3 1994
1995 …Run Code Online (Sandbox Code Playgroud) 在具有大量条形的条形图中,会出现条形之间的间距和/或条形宽度变得不一致的问题,并且也会随着图的宽度的变化而变化。
set.seed(23511)
dat <- data.frame(x = 1:540, y = rnorm(540))
library(ggplot2)
ggplot(dat) +
geom_bar(aes(x = x, y = y), stat = "identity")
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?我尝试调整width整体情节大小,但无济于事。
为了回应 alistaire 的评论,这里是 RStudio 的前几个栏的屏幕截图。查看前 10 个值..
x y
1 1 0.9450960
2 2 0.9277378
3 3 0.4371033
4 4 -1.0333073
5 5 2.0473397
6 6 0.8174123
7 7 0.4277842
8 8 -0.4336887
9 9 0.2156801
10 10 0.4918345
Run Code Online (Sandbox Code Playgroud)
..对我来说,显然前 3 个正值的条形之间有空间/条形比第二组 3 个正值更窄,其中条形之间没有空间/条形更宽。
我的目标是将特定文件夹中的所有txt文件导入列表.
所以我这样做:
setwd(".../folder")
data <- list.files(pattern = "\\.txt$")
lis <- lapply(data, read.csv)
Run Code Online (Sandbox Code Playgroud)
但是,我想避免使用setwd().所以我可以这样做:
data <- list.files(path = ".../folder", pattern = "\\.txt$")
Run Code Online (Sandbox Code Playgroud)
但随后的过程中,我得到一个错误信息没有这样的文件或目录的read.csv外观在错误的目录.如何指定文件夹与导入所有文件data?
我想使用ggplot和stat_contour为的两类数据显示等高线图facet_grid。我想根据数据突出显示特定级别。这是一个使用常规volcano数据的类似虚拟示例。
library(dplyr)
library(ggplot2)
v.plot <- volcano %>% reshape2::melt(.) %>%
mutate(dummy = Var1 > median(Var1)) %>%
ggplot(aes(Var1, Var2, z = value)) +
stat_contour(breaks = seq(90, 200, 12)) +
facet_grid(~dummy)
Run Code Online (Sandbox Code Playgroud)
假设在每个因子水平(我想是东半和西半)内,我想找到火山的平均高度并将其显示出来。我可以手动计算:
volcano %>% reshape2::melt(.) %>%
mutate(dummy = Var1 > median(Var1)) %>%
group_by(dummy) %>%
summarise(h.bar = mean(value))
# A tibble: 2 × 2
dummy h.bar
<lgl> <dbl>
1 FALSE 140.7582
2 TRUE 119.3717
Run Code Online (Sandbox Code Playgroud)
这告诉我,每半部分的平均高度分别为141和119。我可以在两个面上都画出这两个高度,而不仅仅是在每侧上画出适当的高度。
v.plot + stat_contour(breaks = c(141, 119), colour = "red", size …Run Code Online (Sandbox Code Playgroud) 当使用 指定值作为 phi 时fixed = TRUE,如何为每个主题设置固定值(例如,主题 1 的值 = 0.7,主题 2 的值 = 0.5 等)?
library(nlme)
mod <- gls(rate ~ pressure,
data = Dialyzer,
corr = corAR1(form = ~ 1 | Subject, value = 0.7, fixed = TRUE))
Run Code Online (Sandbox Code Playgroud) 这是我的df
df <- structure(structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), y = c(NA, NA, NA, NA, 1, NA, NA, NA, 1, 2, NA, NA, 1, 2, 3, NA, 2, 2, 3, 4, NA, 3, 3, 4, 5), x = c(1L, 2L, 3L, 4L,5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, …Run Code Online (Sandbox Code Playgroud) 我试图用每个方面绘制有自己的传奇的方面.但是,我遇到了一些麻烦,无法正确对齐.
dat <- structure(list(group1 = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), group2 = structure(c(1L, 2L, 1L, 3L,
2L), .Label = c("a", "b", "c"), class = "factor"), x = c("1",
"2", "3", "4", "2"), y = c("1", "2", "3", "4", "3")), .Names = c("group1",
"group2", "x", "y"), row.names = c(NA, 5L), class = "data.frame")
dat <- split(dat, f = dat$group1)
library(ggplot2)
p1 <- ggplot(dat$A) +
geom_point(aes(x=x, y=y, colour=group2)) +
facet_wrap(~group1) +
guides(colour=guide_legend(nrow=2)) +
scale_colour_manual(values=c(a = …Run Code Online (Sandbox Code Playgroud) 我有这个数据帧:
df <- data.frame(group=c("A", "A", "B", "B"), year=c(1980, 1986, 1990, 1992))
group year
1 A 1980
2 A 1986
3 B 1990
4 B 1992
Run Code Online (Sandbox Code Playgroud)
我想通过以下方式修改它:
这将是结果:
group year pre
1 A 1978 pre1980
2 A 1979 pre1980
3 A 1984 pre1986
4 A 1985 pre1986
5 B 1988 pre1990
6 B 1989 pre1990
7 B 1990 pre1992
8 B 1991 pre1992
Run Code Online (Sandbox Code Playgroud)
添加新列很容易..
df$pre <- paste("pre", df$year, sep="")
Run Code Online (Sandbox Code Playgroud)
但我仍然坚持如何添加相应年份的新行(当然创建一个全新的数据框架也同样好).任何提示?