我正在以半小时为间隔绘制时间序列数据,使用 geom_col() 显示每个间隔内计数的鸟类数量。ggplot2 绘制以x 轴刻度线为中心的每个条形图,但我需要每个条形图都从每个刻度线的右侧开始。换句话说,我需要每个条形跨越其相应的半小时间隔的宽度。
我在这些帖子中尝试了建议的解决方案,但没有运气:
下面是重现问题的示例代码:
``` r
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(ggplot2)
library(tidyverse)
df <- data.frame(
date = c("2019-05-16", "2019-05-16", "2019-05-16", "2019-05-16", "2019-05-16", "2019-05-16", "2019-05-16", "2019-05-16"),
time = c("16:30:00", "17:00:00", "17:30:00", "18:00:00", "18:30:00", "19:00:00", "19:30:00", "20:00:00"),
count = c(5, 100, 14, 342, 59, 321, 44, 98),
stringsAsFactors = …Run Code Online (Sandbox Code Playgroud) 我写了以下函数来制作一个定制的堆积图:
stacked_plot <- function(data, what, by = NULL, date_col = date, date_unit = NULL, type = 'area'){
by <- enquo(by)
what <- ensym(what)
date_col <- ensym(date_col)
date_unit <- enquo(date_unit)
if (!rlang::as_string(date_col) %in% names(data)){
return(cat('Nie odnaleziono kolumny "', as_string(date_col), '".', sep = ''))
}
if (!rlang::quo_is_null(date_unit)){
data <- data %>%
mutate(!!date_col := floor_date(!!date_col, unit = !!date_unit, week_start = 1))
}
if (!rlang::quo_is_null(by)) {
data <- data %>%
filter(!is.na(!!by)) %>%
group_by(!!date_col, !!by) %>%
summarise(!!what := sum(!!what, na.rm = TRUE)) %>%
ungroup() %>% …Run Code Online (Sandbox Code Playgroud) 我想在 geom_col 图上仅在模态条(最高峰)上方放置一个标签,给出 x 轴值 (CAG)。这是一个例子,但我只能让它标记每个峰值。
x <- seq(-20, 20, by = .1)
y <- dnorm(x, mean = 5.0, sd = 1.0)
z <- data.frame(CAG = 1:401, height = y)
ggplot(z, aes(x=CAG, y=height)) +
geom_col() +
geom_text(aes(label = CAG))
Run Code Online (Sandbox Code Playgroud)
如果您能帮助我仅标记最高峰,我将非常感激
有了这个数据:
df <- data.frame(value =c(20, 50, 90),
group = c(1, 2,3))
Run Code Online (Sandbox Code Playgroud)
我可以得到一个条形图:
df %>% ggplot(aes(x = group, y = value, fill = value)) +
geom_col() +
coord_flip()+
scale_fill_viridis_c(option = "C") +
theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)
但我希望这些条形的颜色根据它们在 中的相应值而变化value。
我已经设法使用以下方法更改它们geom_raster:
ggplot() +
geom_raster(aes(x = c(0:20), y = .9, fill = c(0:20)),
interpolate = TRUE) +
geom_raster(aes(x = c(0:50), y = 2, fill = c(0:50)),
interpolate = TRUE) +
geom_raster(aes(x = c(0:90), y = 3.1, fill = c(0:90)),
interpolate = …Run Code Online (Sandbox Code Playgroud) 我正在使用ggplot绘制条形图。如何更改条形图中组的顺序?在以下示例中,我希望将 type=1984 作为第一个条形堆栈,然后将 type=1985 放在 1984 之上,依此类推。
series <- data.frame(
time = c(rep(1, 4),rep(2, 4), rep(3, 4), rep(4, 4)),
type = c(1984:1987),
value = rpois(16, 10)
)
ggplot(series, aes(time, value, group = type)) +
geom_col(aes(fill= type))
Run Code Online (Sandbox Code Playgroud)
使用更改顺序series<- series[order(series$type, decreasing=T),]只会更改图例中的顺序,而不是绘图中的顺序。