我有一个数据框,我想以 4 个方面(按 ID)的形式呈现,并且对于每个方面,用矩形突出显示数据。
我的数据是:
dt<-data.frame(ID=c(rep(c("1","2","3","4"),2)),var=c(480,1180,170,130,500,1180,1450,750),
Method=c(rep(c("A","B"),each=4)))
Run Code Online (Sandbox Code Playgroud)
我能找到的所有其他相关帖子都参考了创建一个新的数据框,其中每个矩形的信息作为单独的行。因此,我创建了以下数据框:
rect<-data.frame(xmin = c(0.8, 0.8,0.8,0.8), xmax = c(2.2, 2.2, 2.2, 2.2),
ymin = c(430, 1130, 120, 80), ymax = c(550, 1230, 1500, 800),
alpha = c(.1, .1, .1, .1),
fill = c("red", "green","orange","blue"))
Run Code Online (Sandbox Code Playgroud)
我的 ggplot 代码是:
ggplot(dt,aes(x=Method,y=var)) +
geom_point() +
annotate("rect", xmin = rect$xmin, xmax = rect$xmax, ymin = rect$ymin, ymax = rect$ymax,
alpha = 0.1, fill = "green")+
facet_wrap(~ID,ncol=4)+
theme_bw()
Run Code Online (Sandbox Code Playgroud)
geom_rect 选项对我来说根本不起作用。我只是收到有关缺少变量的错误消息。我已将 ID 和 Method 列添加到 rect 数据框中,但这只会引发 var 变量不存在的问题。我考虑过将其全部合并,但我不确定这是否能解决问题。这是我尝试与 geom_rect …
我正在开展一个项目,其中一小部分是绘制一张世界地图,其中包含我列表中的 135 个国家/地区。我还有一个清单,上面写着它们是否已开发。
我如何在世界地图上用不同的颜色来表示发展状态?
我的数据看起来像这样
Country Code Developed
Brazil BRA 1
Singapore SIN 3
France FRA 1
Poland POL 2
Run Code Online (Sandbox Code Playgroud)
我从另一个问题中获取了下面的图片,但理想情况下,它看起来像这样,但有更多的国家和 3 种不同的颜色。
谢谢
我正在尝试创建一个正常的十六进制图,但我不想按默认值对图进行着色,而是希望按第三个变量的平均值对图进行着色。在我的特定情况下,我无法使用 stat_summary_hex 函数。
library(ggplot2)
library(hexbin)
x <- rnorm(1e4, 0, 5)
y <- rnorm(1e4, 0, 10)
z <- rnorm(1e4, 20, 1)
data.frame(x, y, z) %>%
ggplot(mapping = aes(x = x, y = y, z = z)) +
geom_hex(bins = 20)
Run Code Online (Sandbox Code Playgroud) 将数据按类别(样本 A 和 B)分开,制作 2 层,一层用于点,一层用于线。我想按指示点颜色的类别来分隔数据,并分隔线条,但颜色与用于点的颜色不同。
library(ggplot2)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
geom_line(aes(color = Sample)) +
scale_color_manual(values = c("red", "blue")) + #for poits
scale_color_manual(values = c("orange", "purple")) #for lines
Run Code Online (Sandbox Code Playgroud) 我很疑惑。例如,当使用 ggplot2 时,许多人使用 geom_jitter 向箱线图添加点。至少据我所知,它应该在 Y 轴上保留值,在 X 轴上保留抖动值。
今天在两组中使用它,每组 3 个点,所有相同的值,我看到它在 Y 轴上抖动值。
library(ggplot2)
condition = c(rep("A", 3), rep("B", 3))
fraction = c(rep(100, 3), rep(100, 3))
df = data.frame(condition, fraction)
ggplot(df, aes(condition, fraction))+
geom_jitter(width = 0.2)+
labs(title = "",
x = "", y = "fraction")+
ylim(95,105)+
theme_classic()
Run Code Online (Sandbox Code Playgroud)
图表如下(抱歉太新了,显然无法发布图像,所以这是一个链接):
任何人?