我已經為每個點分配了形狀和顏色,我想根據組資料繪制圖例,但是我無法添加圖例..
library(datasets)
library(tidyverse)
library(reshape2)
name <- c("S1","S1","S1","S2","S2","S3","S3","S3","S4","S4","S5")
x <- c(1,5,9,8,5,6,7,4,3,6,4)
y <- c(3,8,9,5,7,5,3,8,9,3,4)
Shape <- c(21,21,21,22,22,23,23,23,24,24,25)
Color <- c("red","red","red","blue","blue","green","green","green","purple","purple","black")
df <- data.frame(x,y,name,Shape,Color)
graph1 <- ggplot(df,aes(x,y,fill = Color, shape = Shape))
geom_point(size = 4)
scale_shape_identity()
scale_fill_identity()
graph1
x 和 y 是主要資料。名字就是組。形狀和顏色是我為所有這些點分配的形狀和顏色。如何按組繪制圖例?
uj5u.com熱心網友回復:
我認為您正在尋找:
ggplot(df, aes(x = x ,y = y, fill = name, shape = name))
geom_point(size = 4)
scale_fill_manual(values = unique(df$Color))
scale_shape_manual(values = unique(df$Shape))
uj5u.com熱心網友回復:
您缺少aes
引數color
:-)
library(datasets)
library(tidyverse)
library(reshape2)
name <- c('S1', 'S1', 'S1', 'S2', 'S2', 'S3', 'S3', 'S3', 'S4', 'S4', 'S5')
x <- c(1, 5, 9, 8, 5, 6, 7, 4, 3, 6, 4)
y <- c(3, 8, 9, 5, 7, 5, 3, 8, 9, 3, 4)
Shape <- c(21, 21, 21, 22, 22, 23, 23, 23, 24, 24, 25)
Color <- c('red', 'red', 'red', 'blue', 'blue', 'green', 'green', 'green', 'purple', 'purple', 'black')
df <- data.frame(x, y, name, Shape, Color)
graph1 <- ggplot(df, aes(x, y, fill = Color, color = name, shape = Shape))
geom_point(size = 4)
scale_shape_identity()
scale_fill_identity()
graph1
您會注意到,使用組 ( name
) 作為圖例的基礎會使該圖變得混亂,但無論如何,您都可以添加color
引數以確保您的圖例被渲染。
如果將Color
其用作color
引數的值,則會得到以下資訊:
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520904.html