[英]How to add a legend after updating a scatter plot with the symbols() command in r
我使用以下数据在R中创建了一个气泡图/散点图:
和以下代码:
my_data_set <- read.csv("c:/Users/Person/Desktop/my_data_set.csv")
View(my_data_set)
plot(my_data_set$Analysis_Vs_Presentation, my_data_set$Flexibility)
IScolors <- c("#e6f598", "#66c2a5")
TypeLevels <- as.numeric(my_data_set$Type)
symbols(my_data_set$Analysis_Vs_Presentation, my_data_set$Flexibility, circles=sqrt(my_data_set$Easiness), inches=0.8, bg = IScolors[TypeLevels], fg="black", xlab="Presentation", ylab="Flexibility", main="Comparison of 5 Data Analytics Tools", xlim=c(0, 11), ylim=c(0, 11))
text(my_data_set$Analysis_Vs_Presentation, my_data_set$Flexibility, my_data_set$Tool, cex=1)
这给出了一个气泡图散点图,其中气泡大小取决于Easiness的值,气泡颜色则取决于Type的值。
我想添加一个图例以显示气泡的颜色。 我尝试使用此:
legend("bottomright", legend=my_data_set$Type, col=IScolors, cex=0.75)
并且在右下角显示了图例,但它仅列出了Type属性的5个值。
我如何要求它显示列出Type属性的2个不同值以及图表中使用的关联颜色的内容?
更新:克里斯-我尝试了您的建议后,我看到了一个图例,但它显示了所有5个值,而不仅仅是2个不同的值:
由于您没有提供数据来复制图表,因此很难回答。 但是似乎您可能想尝试使用fill
参数。 例如:
legend("bottomright", legend=my_data_set$Type, fill=IScolors, cex=0.75)
好的,我已经麻烦了重新创建代码以查看其工作方式。 这是解决方案-非常简单,如果您所追求的只是这两种类型的两种颜色,对吗? 这实际上是您的代码; 更改的位如下:
df <- data.frame(
Tool = c("R", "GGPlot2", "Tableau", "D3", "Excel"),
Flex = c(6,8,7,10,2),
Type = c("static", "static", "interactive", "interactive", "static"),
Easi = c(6,5,10,1,7),
Ana_v_Pres = c(1,2,5,10,3)
)
View(df)
plot(df$Ana_v_Pres, df$Flex)
IScolors <- c("#e6f598", "#66c2a5")
TypeLevels <- as.numeric(df$Type)
symbols(df$Ana_v_Pres, df$Flex, circles=sqrt(df$Easi), inches=0.8,
bg = IScolors[TypeLevels], fg="black", xlab="Presentation",
ylab="Flexibility", main="Comparison of 5 Data Analytics Tools",
xlim=c(0, 11), ylim=c(0, 11))
text(df$Ana_v_Pres, df$Flex, df$Tool, cex=1)
现在进行更改:只需定义要在图例键中显示的两个标签,然后为其指定col
和fill
参数即可:
legend("bottomright", c("static", "interactive"), col=IScolors, fill=IScolors, cex=0.75)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.