简体   繁体   English

在R中使用symbol()命令更新散点图后如何添加图例

[英]How to add a legend after updating a scatter plot with the symbols() command in r

I've created a bubble chart / scatter plot in R using the following data: 我使用以下数据在R中创建了一个气泡图/散点图:

View my_data_set 查看my_data_set

and following code: 和以下代码:

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)

which gives me a bubble chart scatter plot with differently sized bubbles depending on the value of Easiness, and a bubble colour depending on the value of Type. 这给出了一个气泡图散点图,其中气泡大小取决于Easiness的值,气泡颜色则取决于Type的值。

我的气泡散点图的图片

I want to add a legend to show what the colour of the bubble means. 我想添加一个图例以显示气泡的颜色。 I tried using this: 我尝试使用此:

legend("bottomright", legend=my_data_set$Type, col=IScolors, cex=0.75)

and that displayed a legend in the bottom right, but it just listed the 5 values of the Type attribute. 并且在右下角显示了图例,但它仅列出了Type属性的5个值。

How do I ask it to display something that lists the 2 distinct values of the Type attribute, and the associated colour used in the chart? 我如何要求它显示列出Type属性的2个不同值以及图表中使用的关联颜色的内容?

UPDATE: Chris - after I tried your suggestion I see a legend but it shows all 5 values rather than just the 2 distinct values: 更新:克里斯-我尝试了您的建议后,我看到了一个图例,但它显示了所有5个值,而不仅仅是2个不同的值:

screenshot of plot with added legend 添加图例的情节的屏幕截图

A little difficult to answer since you are not giving data to reproduce the chart. 由于您没有提供数据来复制图表,因此很难回答。 But it seems you might want to try using the fill argument. 但是似乎您可能想尝试使用fill参数。 For example: 例如:

legend("bottomright", legend=my_data_set$Type, fill=IScolors, cex=0.75)

Ok, I've taken the trouble of recreating your code to see how it might work. 好的,我已经麻烦了重新创建代码以查看其工作方式。 Here's the solution--quite simple, iff what you are after is just the two colors for the two types, right? 这是解决方案-非常简单,如果您所追求的只是这两种类型的两种颜色,对吗? This is effectively your code; 这实际上是您的代码; the changed bit follows below: 更改的位如下:

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)

Now for the change: it's just that you define the two labels to be shown in the legend key and assign the col and fill arguments to it: 现在进行更改:只需定义要在图例键中显示的两个标签,然后为其指定colfill参数即可:

legend("bottomright", c("static", "interactive"), col=IScolors, fill=IScolors, cex=0.75)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM