相关疑难解决方法(0)

ggplot缩放颜色渐变到数据范围之外的范围

我正在寻找一种方法来拉伸两个值之间的颜色渐变并标记图例,而不管数据集中的数据值范围如何.基本上,是否有功能等同ylim() 于颜色渐变?

给定通常在-1和1之间绘制az值的代码,如果中断在数据范围内,我可以绘制并标记渐变:

library(ggplot2)

#generator from http://docs.ggplot2.org/current/geom_tile.html
pp <- function (n, r = 4) { 
  x <- seq(-r * pi, r * pi, len = n)
  df <- expand.grid(x = x, y = x)
  df$r <- sqrt(df$x^2 + df$y^2)
  df$z <- cos(df$r^2) * exp(-df$r / 6)
  return(df)
}

t <- pp(30)
summary(t)
b <- c(-.5, 0, .5)
colors <- c('navyblue', 'darkmagenta', 'darkorange1')
p <- ggplot(data = t, aes(x = x, y = y))+
  geom_tile(aes(fill = z))+
  scale_fill_gradientn(colors = colors, …
Run Code Online (Sandbox Code Playgroud)

r data-visualization colors ggplot2

25
推荐指数
1
解决办法
3万
查看次数

使用端点/ ggplot的中断计算设置ggplot网格线

我有一个ggplot问题,我正在尝试以非常小的外观制作线条图.我已经摆脱了传说,转而支持每行右侧的文字标签.如果标签不是那么长,它可能不那么明显,但如果网格线停在最大x值(在这种情况下,在2015年),我会更好.

library(tidyverse)

df <- structure(list(industry = c("Accommodation & Food Services", "Construction", "Health Care & Social Asst.", "Retail Trade", 
        "Accommodation & Food Services", "Construction", "Health Care & Social Asst.", "Retail Trade"), 
    year = c(2002L, 2002L, 2002L, 2002L, 2015L, 2015L, 2015L, 2015L), 
    value = c(6.977, 5.264, 17.065, 14.528, 8.032, 4.648, 20.547, 12.568)), 
    class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L), 
    .Names = c("industry", "year", "value"))

ggplot(df, aes(x = year, y = value, color = industry)) +
  geom_line() +
  geom_text(aes(label …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
242
查看次数

如何保留默认轴标签但在 ggplot2 中添加附加标签

我想保留ggplot2下面为 Y 轴提供的默认标签,但始终有一个 Y 轴刻度和/或标签y = 100来突出显示水平线截距。

library(ggplot2)

maxValue <- 1000

df <- data.frame(
  var1 = seq(1, maxValue, by = 25),
  var2 = seq(1, maxValue, by = 50)
)

ggplot(df, aes(x = var1, y = var2)) +
  geom_point() +
  geom_hline(yintercept = 100, color = "red")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 4 月 9 日创建(v2.0.1.9000)

预期输出:

在此输入图像描述

请注意,maxValue可以是任何东西。所以仅仅增加 100 步的解决方案是行不通的。例如:

plot <- plot +
      scale_y_continuous(
        breaks = seq(0, max(df$y) + 100, 100),
        labels = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

1
推荐指数
1
解决办法
503
查看次数

标签 统计

ggplot2 ×3

r ×3

colors ×1

data-visualization ×1