这里是一些测试数据:
y <- c(1:10, 6:15)
b <- c(rep(c("A", "B"), each=10))
x <- 1:10
df <- data.frame(b, x, y)
Run Code Online (Sandbox Code Playgroud)
和一个测试图:
library(ggplot2)
library(directlabels)
p1 <- ggplot(df, aes(x=x, y=y, colour=b)) + geom_line()
direct.label(p1, list("first.points", hjust=-1, vjust=-0.5))
Run Code Online (Sandbox Code Playgroud)

我希望标签的背景为白色(在文本周围的矩形中)。有办法实现吗?我试过fill="white",colour="white",background="white",什么都没有发生..
我有这个数据帧:
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)
但我仍然坚持如何添加相应年份的新行(当然创建一个全新的数据框架也同样好).任何提示?
我想facet_grid用不平衡的观测值绘制一个用于构面的变量组合的,例如
dat <- data.frame(x = 1:6,
y = 0:5,
group = rep(c("A", "B"), each = 3),
var = rep(c("a", "a", "b"), times = 2))
> dat
group var x y
1 A a 1 0
2 A a 2 1
3 A b 3 2
4 B a 4 3
5 B a 5 4
6 B b 6 5
Run Code Online (Sandbox Code Playgroud)
..并geom_rect在每个方面都添加相同的。
ggplot(dat) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 3, fill …Run Code Online (Sandbox Code Playgroud) 我有一个包含多列的数据框.对于一列,我想计算累积总和,但我有一些缺少值的问题.
#sample data
test <- c(-1.2, 4.6, -8.3, 5, 8, 1, -2, NA, NA, NA, -3, 5.1, 1.9)
test <- as.data.frame(test)
#This gives NA after NAs occurred
sum_test <- lapply(test, FUN=cumsum)
sum_test
$test
[1] -1.2 3.4 -4.9 0.1 8.1 9.1 7.1 NA NA NA NA NA NA
#This continues with adding to pre-NA value after last NA
sum_test <- lapply(test, function(x) ave(x, is.na(x), FUN=cumsum))
sum_test
$test
[1] -1.2 3.4 -4.9 0.1 8.1 9.1 7.1 NA NA NA 4.1 9.2 11.1 …Run Code Online (Sandbox Code Playgroud) 我想使用包直接标签来标记我的情节.但是,我希望标签是每个点的ID.难道真的没有办法选择标注哪个因素或者我错过了哪个因素?
library(ggplot2)
library(directlabels)
df <- structure(
list(id = 1:10,
group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
.Label = c("A", "B"),
class = "factor"),
value1 = c(4, 1, 6, 2, 5, 7, 3, 2, 5, 8),
value2 = c(6, 2, 6, 2, 8, 9, 7, 5, 2, 6)
),
.Names = c("id", "group", "value1", "value2"),
row.names = c(NA, -10L),
class = "data.frame")
p1 <- ggplot(df, aes(x=value1, y=value2)) + geom_point(aes(colour=group))
direct.label(p1)
Run Code Online (Sandbox Code Playgroud)
我有这样一个清单:
df1 <- data.frame(a=c(NA, NA, 1:10), b=c(NA, 1:11))
df2 <- data.frame(a=1:10, b=c(NA,1:9))
mylist <- list(df1, df2)
> mylist
[[1]]
a b
1 NA NA
2 NA 1
3 1 2
4 2 3
5 3 4
6 4 5
7 5 6
8 6 7
9 7 8
10 8 9
11 9 10
12 10 11
[[2]]
a b
1 1 NA
2 2 1
3 3 2
4 4 3
5 5 4
6 6 5
7 7 …Run Code Online (Sandbox Code Playgroud) 我想使用ggarrange(以使 x 轴对齐)排列两个刻面图。
library(egg)
library(ggplot2)
p1 <- ggplot(warpbreaks) +
geom_bar(aes(x = wool)) +
facet_wrap(~tension, ncol = 2, scales = "free_x") +
theme_bw() +
theme(axis.line = element_line(colour = "black", size = .1),
panel.border = element_blank(),
strip.background = element_blank())
p2 <- ggplot(warpbreaks) +
geom_bar(aes(x = tension)) +
facet_wrap(~wool) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size = .1),
panel.border = element_blank(),
strip.background = element_blank())
ggarrange(p1, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
效果很好,但不幸的是垂直轴线消失了。使用 时不会发生这种情况grid.arrange,但至少对于我的真实数据,x 轴未对齐,因此我希望使用ggarrange. 有没有办法保持轴线?
我有这样一个包含年度数据的数据框,但是,有些年份没有数据(这里:1956年,1961年至1964年).
dat <- data.frame(Year = c(1950:1955, 1957:1960, 1965:1970),
Val = 1:16)
> dat
Year Val
1 1950 1
2 1951 2
3 1952 3
4 1953 4
5 1954 5
6 1955 6
7 1957 7
8 1958 8
9 1959 9
10 1960 10
11 1965 11
12 1966 12
13 1967 13
14 1968 14
15 1969 15
16 1970 16
Run Code Online (Sandbox Code Playgroud)
我想添加一个变量"Period",其中包含每个时期的最小和最大年份,其中一个时期被定义为一组连续年份(即1950-1955,1957-1960和1965-1970).创建这个变量本身并不是问题,但我仍然坚持如何进行分组.有任何想法吗?
dat %>%
...???... %>%
mutate(Period = paste(min(Year), max(Year), sep = "-"))
Run Code Online (Sandbox Code Playgroud) 这是我之前的问题的后续行动.虽然krlmlr的答案在某种程度上有助于解决我的问题,但仍然存在一个问题 - 标签远离点移动,因此无法看到哪个标签对应于哪个点.有谁知道如何解决这一问题?
test <- structure(list(ID = c(183, 184, 185, 186, 187, 188, 189, 190,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 204
), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 10L, 1L, 1L, 11L, 1L, 10L, 10L, 1L, 1L, 1L), .Label = c("a",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", …Run Code Online (Sandbox Code Playgroud) 我想知道如何修改下面的代码,根据每个级别的年级频率对单元格进行重新排序.
Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- data.frame(Year, Category, Frequency)
library(dplyr)
Data <- group_by(Data,Year) %>%
mutate(pos = cumsum(Frequency) - (0.5 * Frequency))
library(ggplot2)
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
geom_bar(aes(fill = Category), stat="identity", show.legend = FALSE) +
geom_text(aes(label = Frequency, y = pos), size …Run Code Online (Sandbox Code Playgroud) 我想使用 gganimate 对一些数据进行动画处理。以他们的 github 页面为例,我对其进行了一些更改以反映我的情况。X 轴是日期,我希望所有框架的徽标都位于相同位置。
可重现的代码:
library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 400)
(p <- ggplot(gapminder, aes(year, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
annotation_raster(tiger, ymin = 75, ymax = 100, xmin = 1965, xmax = 2005) )
# here the animate part (not needed just for ilustrative purposes)
p + labs(title = 'Year: {frame_time}', x …Run Code Online (Sandbox Code Playgroud)