如何避免 ggplot2 中的这两层重叠?我尝试显示文本,以便它们不会位于点上方。
check_overlap 在避免文本与自身重叠但不与其他图层重叠方面做得很好。
我也尝试过 library geom_text_repel,但是这个 library 不支持check_overlap并显示每个数据点的文本。
但我不需要每个点都有文字,就像check_overlap那样。
ggplot(dat, aes(x = CPI, y = HDI)) +
geom_point(aes(color = Region), shape=21, size=4, position = "identity") +
geom_text(data = dat, aes(label = Country), size=4, check_overlap = TRUE)
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种自动移动 y 轴刻度标签的方法,以便它们在实际绘图区域内左对齐。我喜欢 ggplot 中主题组件的总体灵活性,但在尝试找到通用方法来实现此目的时遇到了困难。
我知道给出负右边距 (*gag*)axis.text.y的组合hjust=0可以实现此效果,但必须手动设置负边距以匹配最长 y 轴刻度标签的宽度。
作为示例,请考虑以下代码:
library(ggplot2)
set.seed(0)
dat <- data.frame(x = 1:100, y = (1:100) + runif(100, -10, 10))
p1 <- ggplot(dat, aes(x, y)) +
geom_line() +
scale_y_continuous("", breaks = c(0, 30, 60, 90),
labels = c(0, 30, 60, "90 units of something")) +
theme(axis.text.y = element_text(hjust = 0,
margin = margin(0, -3.1, 0, 0, 'cm')))
Run Code Online (Sandbox Code Playgroud)
我认为它优雅地将 y 轴标签(例如"units of something")合并到图的主体中,但为了实现它,-3.1必须手动找到最后一行中的 (通过反复试验),这雪上加霜:我不仅使用负边距将文本拉到不想要的地方——我还添加了一些神秘的、脆弱的、硬编码的魔法数字。
有谁知道我可以在哪里找到一个更通用和优雅的解决方案来解决这个问题?
我想看看将因子值转换为数值的地方。我试图通过简单地print在各处添加语句来实现这一点。
geom_tile2 <- function(mapping = NULL, data = NULL,
stat = "identity2", position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomTile2,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}
GeomTile2 <- ggproto("GeomTile2", GeomRect,
extra_params = c("na.rm", "width", "height"),
setup_data = function(data, params) {
print(data)
data$width <- data$width %||% params$width …Run Code Online (Sandbox Code Playgroud) df = data.frame(a = c(0, 0), b = c(17, 15),
c = c(35,37), d = c(55,57),
e = c(80, 85), x = c(1, 2),
w1 = c(20, 30), w2 = c(0.2, 0.3))
ggplot(df) +
geom_boxplot(aes(x = x, ymin = a, lower = b, middle = c, upper = d, ymax = e),
stat = "identity")
Run Code Online (Sandbox Code Playgroud)
我有一个数据框,包含箱图的每个分位数的值,(ae).是否可以使用列w1或w2来定义ggplot中箱线图的宽度?
我期望的结果是与使用varwidth中graphics::boxplot,但自定义的宽度.
graphics::boxplot(mpg~gear, mtcars, varwidth = T)
Run Code Online (Sandbox Code Playgroud)
不要认为这是重复的,因为看起来这个weight论点不起作用stat = identity.
我想使用经度和纬度点在 R 中绘制美国多个州的轮廓。现在我只能画出每个州的轮廓,所以我想知道如何画出美国多个州的轮廓(仅限外线)。
library(tidyverse)
library(knitr)
library(broom)
library(stringr)
library(modelr)
library(forcats)
library(ggmap)
states <- map_data("state")# %>% as_tibble()
counties <- map_data("county")
il_df <- filter(fifty_states, id == "illinois")
midwest <- subset(fifty_states, id %in% c("illinois", "indiana", "iowa",
"kansas", "michigan", "minnesota",
"missouri", "nebraska", "north dakota",
"ohio", "south dakota", "wisconsin"))
il_mid <- ggplot(data = midwest, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
il_mid
Run Code Online (Sandbox Code Playgroud)
我想知道如何绘制下图中的粉红色线。
我想绘制连续变量和分类变量(geom_boxplotwith ggplot2)之间关系的箱线图,这适用于几种情况(facet_wrap)。很简单:
data("CO2")
ggplot(CO2, aes(Treatment, uptake) ) +
geom_boxplot(aes(Treatment, uptake),
col="black", fill="white", alpha=0, width=.5) +
geom_point(col="black", size=1.2) +
facet_wrap(~Type, ncol=3, nrow=6, scales= "free_y") +
theme_bw() +
ylab("Uptake")
Run Code Online (Sandbox Code Playgroud)
This is quite nice with this toy dataset, but applied to my own data (where facet_wrap enables me to plot 18 different graphs) the y-axes are hardly readable, with varying number of y-ticks and varying spacing between them:
What could be a nice way to harmonize the …
我想知道是否有人知道允许对热图进行部分行标记的包。我目前正在使用 pheatmap() 来构建我的热图,但我可以使用任何具有此功能的包。
我有很多行差异表达基因的图,我想标记其中的一个子集。有两个主要的事情需要考虑(我能想到的):
下面是一个部分解决方案的例子,它实际上只能达到一半,但我希望能说明我想要做的事情。
set.seed(1)
require(pheatmap)
require(RColorBrewer)
require(grid)
### Data to plot
data_mat <- matrix(sample(1:10000, 300), nrow = 50, ncol = 6)
rownames(data_mat) <- paste0("Gene", 1:50)
colnames(data_mat) <- c(paste0("A", 1:3), paste0("B", 1:3))
### Set how many genes to annotate
### TRUE - make enough labels that some overlap
### FALSE - no overlap
tooMany <- T
### Select a few genes to annotate
if (tooMany) {
sigGenes_v <- paste0("Gene", c(5,20,26,42,47,16,28))
newMain_v <- "Too Many Labels"
} else …Run Code Online (Sandbox Code Playgroud) 我是 R 新手,对 ggplot 和以下数据集(从较大的集合中选择作为代表)有问题,其中geom_errorbar条形图与平均点(使用 geom_point)不对齐,并且在某些情况下水平条形图与垂直条形不对齐中geom_errorbar,这样横杆就不再是顶部和底部带有横杆的“I”,而是与垂直线分离或偏离中心。
ggplot我已经查看了, geom_point, geom_errorbar, position_jitter(dodge, jitterdodge)的所有手册页。我还从这里尝试了很多事情,例如改变geom_point和geom_errorbar调用中的美观(例如如何使 geom_bar 中的闪避与 geom_errorbar, geom_point 中的闪避一致)
这是一个基本数据集:
df <- structure(list(
Test = c("A", "B", "C", "D", "A", "C", "D"),
mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456),
sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668),
lower = c(2, 102.839669, 2, 2, 56.405654, 2, 6.707124),
upper = c(0, 98.747699, 0, 0, 46.825548, 0, -1.79421)),
row.names …Run Code Online (Sandbox Code Playgroud) 我正在使用facet_wrapggplot绘制时间序列值及其百分比:
对于下图,上图是值,下图是百分比变化。我希望下图中的 y 轴为“%”。通常在 ggplot 我会做类似的事情
+ scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)
但是由于我使用的是 facet_wrap,我如何指定我只希望 2 个图的 y 轴标签之一是百分比?
PS这是生成此图的代码:
library(data.table)
library(ggplot2)
library(scales)
library(dplyr)
pct <- function(x) {x/lag(x)-1}
Dates = seq(from = as.Date("2000-01-01"),
to =as.Date("2018-10-01"),
by = "1 month")
set.seed(1024)
this_raw = data.frame(CM = Dates,
value = rnorm(n = length(Dates)),
variable = rep("FAKE",length(Dates)))
this_diff = na.omit(as.data.table(this_raw %>%
group_by(variable) %>%
mutate_each(funs(pct), c(value))))
this_diff$type = "PerCng"
this_raw$type = "RAW"
plot_all = rbindlist(list(this_raw,this_diff))
plot_all$type = factor(plot_all$type, levels = c("RAW", "PerCng"))
out_gg = plot_all %>% …Run Code Online (Sandbox Code Playgroud) 我有一个巨大的文件(下面是一小部分数据),如下所示,我想绘制一个 PCA,我可以使用 PCA 函数绘制 PCA,但它看起来有点乱,因为我有 200 列,所以我想也许 t- SNE 或 UMAP 效果更好,但我无法使用它们进行绘图。
我想在图中显示列(列名称)之间的关系和聚类。事实上,我从不同的研究中收集了 A、B 和...数据,我喜欢检查它们之间是否存在批次效应。
如果有人能帮助我,我将不胜感激!
DF:
A B C D
1:540450-541070 0.12495878 0.71580434 0.65399319 1.04879290
1:546500-548198 0.41064192 0.26136554 0.11939805 0.28721360
1:566726-567392 0.00000000 0.06663644 0.45661687 0.24408844
1:569158-570283 0.34433086 0.27614141 0.54063437 0.21675053
1:603298-605500 0.07036734 0.42324126 0.23017472 0.29530045
1:667800-669700 0.20388011 0.11678913 0.00000000 0.12833913
1:713575-713660 7.29171225 12.53078648 2.38515165 3.82500941
1:724497-727160 0.40730086 0.26664585 0.45678834 0.12209005
1:729399-731900 0.74345727 0.49685579 0.72956458 0.32499580
Run Code Online (Sandbox Code Playgroud) r ×10
ggplot2 ×9
annotations ×1
facet-wrap ×1
geom-text ×1
ggproto ×1
heatmap ×1
label ×1
pca ×1
pheatmap ×1
runumap ×1
scatter-plot ×1
time-series ×1