R按系数手动设置形状 [英] R manually set shape by factor
问题描述
A <-c('a','b','c','d','e')
类型< - 因子(A)
B < - c(1,2,3,4,5)
C < - c(6,7,8,9, 10)
D < - c(1,2,1,2,3)
ABC < - data.frame(B,C,D,类型)
library(ggplot2)
ggplot(ABC,aes(x = B,y = C,size = D,color = as.factor(types),label = types,shape = as.factor(types ))+
geom_point()+ geom_text(size = 2,hjust = 0,color =black,vjust = 0)+
scale_size_area(max_size = 20,D,breaks = c (100,500,1000,3000,5000))+
scale_x_log10(lim = c(0.05,10),breaks = c(0.1,1,10))+ scale_y_continuous(lim = c(0,30000000))+
scale_shape_manual(values = c(15,18,16,17,19))`
绘制这个,你会有因素ae,有颜色和形状归因于他们。
在我的代码中,我使用scale_shape_manual来设置形状,它们被定义为因素的顺序是a,b,c,d,e,我的值是15,18,16,17,19,所以a = 15(一个方块),b = 18等等等等。
我想按因子设置这些形状。我的数据每天都会改变,因素会有不同的顺序,但我总是希望同样的因素具有相同的形状。
所以很明显,这段代码不起作用,但例如:
$ b
scale_shape_manual(values =('a'= 15,'b'= 18,'c'= 16,'d' = 17,'e'= 19))
如果我也可以对颜色也做同样的操作, b
$ b
谢谢 解决方案
如果我正确地理解了你,大多数)五个类别a - e,并且您希望这些类型的形状和颜色在数据集中保持一致。这里有一种方法(注意: Asked this question the other day but no one could visualize my question so ive made an example. Plotting this you will there are factors a-e that have colours and shapes attributed to them. In my code I use scale_shape_manual to set the shapes and they are definded by sequence ie the order of factors is a,b,c,d,e and my values are 15,18,16,17,19 so a=15 (a square), b=18 etc etc I would like to set these shapes by factor. My data will be changing each day and the factors will be in different orders but I always want the same factors to have the same shapes. So obviously this code doesnt work but something like: Would be helpful if I could do the same for colour too. Thanks If I'm understanding you correctly, there will always be (at most) the five categories "a" - "e", and you want the shapes and colors for these to be consistent across datasets. Here is one way (note: 这篇关于R按系数手动设置形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! gg_color_hue(...)
来自
#设置形状
形状< - c (15,18,16,17,19)
名称(形状)< - 字母[1:5]
#设置颜色
gg_color_hue< - function( n){#ggplot默认颜色
hues = seq(15,375,长度= n + 1)
hcl(h = hues,l = 65,c = 100)[1:n]
颜色< -gg_color_hue(5)
名称(颜色)< - 名称(形状)
#原始数据
ggplot(ABC,aes (x = B,y = C,size = D,color = types,label = types,shape = types))+
geom_point()+ geom_text(size = 2,hjust = 0,color =black ,vjust = 0)+
scale_size_area(max_size = 20,D,breaks = c(100,500,1000,3000,5000))+
scale_x_log10(lim = c(0.05,10),break = c(0.1,1,10))+
scale_y_continuous(lim = c(0,30000000))+
scale_shape_manual(values = shapes)+ scale_color_manual(values = colors)
<$ c $ (b,C,D,types = factor(c(a,a,a,d,e)) )
ggplot(DEF,aes(x = B,y = C,size = D,color = types,label = types,shape = types))+
geom_point()+ geom_text(size = 2) ,hjust = 0,color =black,vjust = 0)+
scale_size_area(max_size = 20,D,breaks = c(100,500,1000,3000,5000))+
scale_x_log10 lim = c(0.05,10),breaks = c(0.1,1,10))+
scale_y_continuous(lim = c(0,30000000))+
scale_shape_manual(values = shapes)+ scale_color_manual值=颜色)
A <- c('a','b', 'c','d','e')
types <- factor(A)
B <- c(1,2,3,4,5)
C <- c(6,7,8,9,10)
D <- c(1,2,1,2,3)
ABC <- data.frame(B,C,D,types)
library(ggplot2)
ggplot(ABC, aes(x=B ,y=C ,size=D, colour=as.factor(types),label=types, shape=as.factor(types))) +
geom_point()+geom_text(size=2, hjust=0,colour="black", vjust=0) +
scale_size_area(max_size=20, "D", breaks=c(100,500,1000,3000,5000)) +
scale_x_log10(lim=c(0.05,10),breaks=c(0.1,1,10))+ scale_y_continuous(lim=c(0,30000000)) +
scale_shape_manual(values=c(15,18,16,17,19))`
scale_shape_manual(values=('a'=15, 'b'=18, 'c'=16, 'd'=17, 'e'=19))
gg_color_hue(...)
is from here):# set up shapes
shapes <- c(15,18,16,17,19)
names(shapes) <- letters[1:5]
# set up colors
gg_color_hue <- function(n) { # ggplot default colors
hues = seq(15, 375, length=n+1)
hcl(h=hues, l=65, c=100)[1:n]
}
colors <- gg_color_hue(5)
names(colors) <- names(shapes)
# original data
ggplot(ABC, aes(x=B ,y=C ,size=D, colour=types,label=types, shape=types)) +
geom_point()+geom_text(size=2, hjust=0,colour="black", vjust=0) +
scale_size_area(max_size=20, "D", breaks=c(100,500,1000,3000,5000)) +
scale_x_log10(lim=c(0.05,10),breaks=c(0.1,1,10))+
scale_y_continuous(lim=c(0,30000000)) +
scale_shape_manual(values=shapes) + scale_color_manual(values=colors)
#new data
DEF <- data.frame(B,C,D,types=factor(c("a","a","a","d","e")))
ggplot(DEF, aes(x=B ,y=C ,size=D, colour=types,label=types, shape=types)) +
geom_point()+geom_text(size=2, hjust=0,colour="black", vjust=0) +
scale_size_area(max_size=20, "D", breaks=c(100,500,1000,3000,5000)) +
scale_x_log10(lim=c(0.05,10),breaks=c(0.1,1,10))+
scale_y_continuous(lim=c(0,30000000)) +
scale_shape_manual(values=shapes) + scale_color_manual(values=colors)