我正在使用这个脚本:
# my data:
df <- data.frame(Somatic = c("PIK3CA", "TP53", "GATA3", "KMT2C", "PTEN", "NCOR1", "ARID1A", "RUNX1", "NF1", "TBX3", "FOXA1", "ERBB2", "AKT1", "PREX2", "CBFB", "INPPL1", "NSD3", "ESR1", "AXIN2", "RAD51C"),
Freq_mut = c(32.6, 32.6, 11.9, 9.3, 5.4, 4.7, 3.8, 3.8, 3.6, 3.1, 2.9, 2.8, 2.5, 2.4, 2.3, 1, 1, 0.8, 0.6, 0.6))
library(randomcoloR)
# set of 20 random colors
colors <- distinctColorPalette(20)
ggplot(df, aes(x = "", y = Freq_mut, fill = Somatic)) +
geom_bar(width = 1, stat = "identity", color = "black") …Run Code Online (Sandbox Code Playgroud) 我有这段代码并且我正在使用 RStudio
library(tidyverse)
library(osmdata)
bbx <- getbb("hawaii")
border <- bbx %>%
opq() %>%
add_osm_feature(key = "place",
value = c("island")) %>%
osmdata_sf()
border
ggplot() +
geom_sf(
data = border$osm_lines,
inherit.aes = FALSE,
color = "black",
size = .4,
alpha = .8
)
Run Code Online (Sandbox Code Playgroud)
当我绘制这个时我有这个
正如您所看到的,我在标签中使用了一个白色矩形,而不是度数符号。
我怎么解决这个问题?
提前致谢
PS:我使用的是 Window11 64 位,RStudio 已更新为默认选项,并且所有软件包均已更新。
我正在尝试应用R/ggplot2:从散点图中折叠或删除 y 轴段,以从绘图的 y 轴中删除 25 到 75 之间的值。但是,当我对图进行分组时,我没有得到所需的结果。
挤压 y 轴的一部分以使绘图清晰的最佳方法是什么?
library(scales)
squish_trans <- function(from, to, factor) {
trans <- function(x) {
if (any(is.na(x))) return(x)
# get indices for the relevant regions
isq <- x > from & x < to
ito <- x >= to
# apply transformation
x[isq] <- from + (x[isq] - from)/factor
x[ito] <- from + (to - from)/factor + (x[ito] - to)
return(x)
}
inv <- function(x) {
if (any(is.na(x))) return(x)
# …Run Code Online (Sandbox Code Playgroud) 我想 purrr::map 一个 df 并为每一列生成一个带有分组变量的箱线图。尝试示例:
mtcars |> select(am, gear, carb) |>
map(~ ggplot() +
geom_boxplot(aes(y = .x, fill = factor(mtcars$cyl))) +
labs(title = .y))
Run Code Online (Sandbox Code Playgroud)
结果:
dots_list(..., title = title, subtitle = subtitle, Caption = Caption, 中的错误:...列表包含的元素少于 2 个
期望的结果:
.x将是来自 df 的向量/场_,分配分组变量cyl.y以前曾在这里使用过我如何 purrr::map 3 列 am、gear 和 carb,为每列生成一个按 cyl 分组/分割并使用 hte 本地管道的箱线图?
在底部发布的代码中,我尝试向现有图例添加一个带有标签“置信区间”的灰色条。有人可以帮我将其添加到图例中吗?如果这可以添加到现有的传说中,那就太好了;如果只为这个项目添加一个新的图例更清晰,那也可以。作为旁注,对于渲染的简单绘图,我的 ggplot 代码看起来相当长且混乱,也许这就是 ggplot 代码总是最终的结果。我想知道 ggplot 与基本 R 图相比是否值得。
代码:
library(dplyr)
library(ggplot2)
library(MASS)
library(survival)
lung1 <- lung %>%
mutate(time1 = ifelse(time >= 500, 500, time)) %>%
mutate(status1 = ifelse(status == 2 & time >= 500, 1, status))
weibCurve <- function(time, survregCoefs) {exp(-(time/exp(survregCoefs[1]))^exp(-survregCoefs[2]))}
fit1 <- survreg(Surv(time1, status1) ~ 1, data = lung1)
lung1.survfit <- survfit(Surv(time1, status1) ~ 1, data = lung1)
lung1.df <- data.frame(time = lung1.survfit$time,
survival = lung1.survfit$surv,
upper_95 = lung1.survfit$upper,
lower_95 = lung1.survfit$lower)
lung1.df %>%
ggplot(aes(x = time, …Run Code Online (Sandbox Code Playgroud) 我想绘制facet_wrap2 个组中的一组,并将 y 轴标签放在左图的左侧,将右侧的标签放在右图的右侧。
这是正常行为。左侧的两个 y 轴标签。
library(tidyverse)
mtcars %>% mutate(let = rep(LETTERS[1:2], times = nrow(mtcars)/2),
make = rownames(mtcars)) %>%
ggplot(aes(y = make, x = mpg)) +
geom_point() +
facet_wrap(~ let, scales = "free")
Run Code Online (Sandbox Code Playgroud)

创建于 2023-05-15,使用reprex v2.0.2
我希望将B图 y 轴标签放在图的右侧,但将A轴标签保留在图的左侧。这可能吗?
我有两个包含三个变量的数据集。我想在世界地图上将其中一个显示为热图,两个显示为饼图。
我使用了下面的代码,但出现了错误。我想知道如何解决它。
library(ggplot2)
library(scatterpie)
world_map <- map_data("world")
df <- data.frame(Country = c("USA", "Canada", "Mexico"),
Cases = c(10, 20, 30))
df_piechart <- data.frame(long = c(-95, -110, -75),
lat = c(37, 60, 20),
size = c(5, 10, 15),
Group1 = c(20, 30, 50),
Group2 = c(30, 40, 30))
ggplot() +
geom_map(dat = world_map, map = world_map, aes(map_id = region), color = "white", fill = "#d3d4d3", linewidth = 0.5, alpha=0.4 ) +
geom_map(dat = df, map = world_map, aes(map_id = Country, fill = …Run Code Online (Sandbox Code Playgroud) 我有 30 个观察值的汇总统计数据,即:最小值、最大值、平均值/中位数、第 25 个百分位数和第 75 个百分位数。如果我有这些数字的基础数据,我可以简单地在 ggplot2 中绘制箱线图,所有这些数字都会丢失,但我只有摘要统计数据。有没有办法只使用这些数字在 ggplot 中绘制箱线图?谢谢。
我有以下示例数据,想要创建一个地平线图,area显示year. 有什么建议使用这样做吗ggplot2?
year <- 1990:2005
area1 <- runif(16, 18,20)
area2 <- runif (16,6,6.7)
area3 <- runif(16, 7,8)
dat <- data.frame(year, area1, area2, area3)
Run Code Online (Sandbox Code Playgroud) ggplot2 ×10
r ×10
facet-grid ×1
facet-wrap ×1
geom-map ×1
lattice ×1
legend ×1
plot ×1
purrr ×1
scatterpie ×1