我有一些質心位置,我想將它們添加到現有的 ggplot 中,但我不確定如何去做。我嘗試創建兩個 ggplots 并嘗試將它們組合起來,但這似乎沒有成功,因為我的質心沒有E導致Error in factor(E) : object 'E' not found
我的目標是讓資料和質心都出現在 plot1 內,這樣我們就可以看到資料和質心位置。
plot1 資料的
uj5u.com熱心網友回復:
主要問題(原因Error in factor(E) : object 'E' not found是提供給您的ggplot()呼叫的標準會被繼承給您以后的geom_point()和geom_label()。但是由于您在那里提供了一個新的data =,它無法找到繼承的E。所以您可以按照@Quinten 的建議進行操作或添加inherit.aes = FALSE,就像我的解決方案一樣。
資料
structure(list(Subject = 1:15, X = c(1L, 1L, 2L, 3L, 3L, 4L,
5L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L), Y = c(1L, 6L, 1L, 9L,
10L, 6L, 6L, 2L, 1L, 9L, 1L, 9L, 10L, 3L, 5L), E1 = c(1L, 1L,
NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, 1L, 1L, 1L, NA), E2 = c(NA,
NA, 2L, 2L, 2L, NA, NA, NA, NA, NA, 2L, NA, NA, NA, 2L)), class = "data.frame", row.names = c(NA,
-15L))
代碼
library(dplyr)
library(ggplot)
data2 <- data %>% filter(!if_all(c(E1, E2), is.na)) %>% mutate(E = ifelse(is.na(E1), E2, E1))
x <- c(2,6.2, 8.8)
y <- c(3.5, 8.8, 2.4)
coords = paste(x , y, sep = ",")
df = data.frame(x, y)
ggplot(data2, aes(X, Y, shape = factor(E)))
geom_point(size = 4)
scale_shape_manual(values = c(8, 3), name = "E")
theme_bw()
geom_point(df, mapping = aes(x, y), col = "blue", size = 3, inherit.aes = FALSE)
geom_label(df, mapping = aes(x .5, y 0.5, label = coords), inherit.aes = FALSE)
輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450636.html
