推理:我想轻松使用现成的连续尺度(来自任何提供scale_..._continuous等的包),用于有序因子类数据,例如mtcars$cyl. 因为这些数据只包含几种离散值,我想直接标记图例键,而不是 bin 限制。怎么做?
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, color = cyl))+
geom_point() +
scale_color_continuous(limits = range(mtcars$cyl),
guide = guide_colorsteps(ticks.colour = "black"))
Run Code Online (Sandbox Code Playgroud)
不想要的:

hacky 方式,实际上是四个 (!) hacks,并且是不想要的。
# Hack 1 - create the colors manually from the palette. This is already annoying.
cont_col <- colorRampPalette(c("#132B43","#56B1F7"))(length(unique(mtcars$cyl)))
# Hack 2- you need to modify the underlying draw_key function
# from https://github.com/tidyverse/ggplot2/issues/2844
draw_key_polygon2 <- function(data, params, size) {
lwd <- min(data$size, min(size) / 4)
grid::rectGrob(
width = grid::unit(1, "npc"),
height …Run Code Online (Sandbox Code Playgroud) 我正在使用ggplot制作一个图形,图例位于图表的水平上方.我的变量有多个图例(即颜色,形状,线型).
+ theme(legend.position = 'top', legend.direction = "horizontal", legend.box = "horizontal")
Run Code Online (Sandbox Code Playgroud)
无论如何,将每个图例的标题放在描述之上而不是侧面
和/或
将密钥放在标签文本上方.
首先,让我们创建一些假数据:
d <- c("2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-03", "2019-04-06", "2019-04-03", "2019-05-07", "2019-05-03", "2019-05-03", "2019-05-03", "2019-05-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-06-03", "2019-07-03", "2019-07-03", "2019-07-04", "2019-08-03", "2019-09-05", "2019-09-03", "2019-09-03", "2019-09-06", "2019-09-08", "2019-10-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-11-03", "2019-12-03", "2019-12-03")
df <- data.frame(dates=as.Date(d))
Run Code Online (Sandbox Code Playgroud)
现在,我创建一个时间序列图:
# aggregate data
df_plot <- df %>% mutate(month = lubridate::floor_date(dates, "month")) %>%
group_by(month) %>% summarise(count = n())
# plot data
ggplot(aes(x = month, y = count), data = df_plot) + geom_line() +
scale_x_date() +
geom_vline(xintercept = …Run Code Online (Sandbox Code Playgroud) 通过引用更新列时,我对以下警告感到困惑fifelse。
fifelse(char == "PL", 2, as.numeric(char)) 中的警告:强制引入的 NA
但没有NA!!
str(mydt)表明类到数字的转换已经成功。
有人可以解释一下吗?
library(data.table)
mydt <- data.table(char = c('1','PL'))
mydt[, newcol := fifelse(char == 'PL', 2, as.numeric(char))]
#> Warning in fifelse(char == "PL", 2, as.numeric(char)): NAs introduced by
#> coercion
mydt
#> char newcol
#> 1: 1 1
#> 2: PL 2
Run Code Online (Sandbox Code Playgroud)
由reprex 包于 2020 年 1 月 20 日创建(v0.3.0)
devtools::session_info()
#> data.table * 1.12.6 2019-10-18 [1] CRAN (R 3.6.1)
Run Code Online (Sandbox Code Playgroud) 我有一个函数列表,其中大部分都采用相同的附加参数 ( na.rm = TRUE)。
我想添加一个不接受此参数的函数 ( length)。是否可以将附加参数仅应用于可以使用它的函数?我想过使用...,但我不确定如何应用它,如果这可能的话。
我正在使用lapply,但对任何选项都感到满意,也超出了基础 R。
x <- c(1:10,NA)
# working example only with functions that take the extra argument
show_stats <- function(x) {
funs <- list(mean = mean, sd = sd)
lapply(funs, function(f) f(x, na.rm = TRUE))
}
show_stats(x)
#> $mean
#> [1] 5.5
#>
#> $sd
#> [1] 3.02765
# sadly not working, because length() only takes one argument
show_stats <- function(x) {
funs <- list(mean = mean, …Run Code Online (Sandbox Code Playgroud) 我是 R 的新手,并且设法在 ggplot 中制作了一个多线图。沿着 x 轴,我有 5 个时间点,它们是离散度量。我想要虚线的后半部分(见附图),有人可以帮我编辑我当前的代码来实现这一点吗?任何帮助将不胜感激。先感谢您!

ggplot(longdata, aes(x = Time, y = Strength, colour = Group)) +
stat_summary(fun = mean,
geom = "point",)+
stat_summary(fun = mean,
geom = "line",
aes(group = Group), size =1,
show.legend = FALSE) +
geom = "errorbar",
width = .1, alpha = 0.3) +
xlab("Testing Timepoint") +
ylab("Isometric Strength (N.m)") +
bbtheme +
scale_colour_manual(name = "Group",
labels = c("Once-weekly", "Twice-weekly"),
values = c("red1", "navyblue")) +
expand_limits(y = 0)
Run Code Online (Sandbox Code Playgroud) 我根据坐标绘制 x 和 y,颜色是第三个连续变量,具有 6 位小数。
ggplot(data, aes(x=x, y=y, color=continuous_variable)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
目前,颜色正在基于 3 位小数创建渐变,但我希望它显示 5。
有没有办法增加 ggplot 用于创建渐变的小数位数?
geom_hline()我使用和创建了一个散点图geom_vline(),该图很好,但图例条目不是我想要的显示方式。图例中的(vline恢复)和hline(阈值)是相互交叉的,令人困惑。我希望恢复图例条目为橙色垂直线,阈值图例条目为水平黑线。
我尝试了其他帖子中建议的几件事,但guide_legend(override.aes())要么show.legend = F更改了上面“类型”部分的图例条目(它删除了行并保留彩色圆圈),要么只是删除了其中一行的图例条目。
这是我当前的代码:
\nggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) + \n geom_point(size = 3) + \n geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) + \n scale_shape_manual(values = c(15, 16, 17)) +\n scale_colour_manual(values = c("chocolate1", "darkcyan")) + \n geom_vline(aes(xintercept = Restauration_year, linetype = "Restoration"), colour = "chocolate1") + \n …Run Code Online (Sandbox Code Playgroud) 在遇到另一个问题后我正在思考这个问题。
library(tidyverse)
set.seed(42)
df <- data.frame(x = cut(runif(100), c(0,25,75,125,175,225,299)))
Run Code Online (Sandbox Code Playgroud)
tidyr::extract很好地分割成由正则表达式定义的组:
df %>%
extract(x, c("start", "end"), "(\\d+),(\\d+)") %>% head
#> start end
#> 1 0 25
#> 2 0 25
#> 3 0 25
#> 4 0 25
#> 5 0 25
#> 6 0 25
Run Code Online (Sandbox Code Playgroud)
字符向量上的所需输出。我知道你可以创建一个新函数,我想知道这是否已经存在。
x_chr <- as.character(df$x)
des_res <- str_split(str_extract(x_chr, "(\\d+),(\\d+)"), ",")
head(des_res)
#> [[1]]
#> [1] "0" "25"
#>
#> [[2]]
#> [1] "0" "25"
#>
#> [[3]]
#> [1] "0" "25"
#>
#> …Run Code Online (Sandbox Code Playgroud) 我的数据:
a <- sample(1:5, 100, replace = TRUE)
b <- sample(1:5, 100, replace = TRUE)
c <- sample(1:10, 100, replace = TRUE)
d <- sample(1:40, 100, replace = TRUE)
df <- data.frame(a, b, c, d)
Run Code Online (Sandbox Code Playgroud)
使用ggplot2,我在 x = a 和 y = b 上创建了散点图,并在二维上加权(通过colour = c和size = d)。请注意,x 和 y 是故意的1:5。
显然,不同大小和颜色的点因此重叠,所以我尝试抖动以避免重叠:
ggplot(df, aes(a, b, colour = c, size = d)) +
geom_point(position = position_jitter())
Run Code Online (Sandbox Code Playgroud)
现在我希望这些点聚集得更紧密,所以我尝试了抖动函数height和的几种组合width,例如
ggplot(df, aes(a, …Run Code Online (Sandbox Code Playgroud) r ×10
ggplot2 ×7
geom-vline ×2
data.table ×1
geom-hline ×1
legend ×1
overlap ×1
plot ×1
scatter ×1
time-series ×1