简体   繁体   English

在R中的散点图中为特定点着色

[英]Colouring a specific point in a scatterplot in R

I'm somewhat new to R and ggplot2. 我是R和ggplot2的新手。 I've been trying to create a scatterplot graph that has one specific point coloured. 我一直在尝试创建一个散点图,该散点图具有一个特定的点着色。 For example, here is my basic data frame 例如,这是我的基本数据框

manager     Confirmed Overturned  keeping  Stands  total
A.J. Hinch     11         24         0       14     49
Angel Hernandez 0          1         0        0      1
Bill Miller     3          1         0        4      8
Bob Melvin      6         16         0        6      28
Brad Ausmus     3         11         0       13      27

With this I can create a simple scatterplot using this code, 这样,我可以使用以下代码创建一个简单的散点图,

p <- ggplot(data = Outcome, aes(x = Overturned, y = total))
p + geom_point()

I know how to add general colour, and add a colour scale, but I don't know how to colour just one point. 我知道如何添加一般颜色并添加色阶,但是我不知道如何仅对一个点着色。 For example, let's say I wanted to colour AJ Hinch blue, and make every other point a different colour (probably grey or black), how would I do that? 例如,假设我想给AJ Hinch涂上蓝色,并让其他每个点变成不同的颜色(可能是灰色或黑色),我该怎么做?

Here is a link to the graph I want to create in Tableau. 这是我要在Tableau中创建的图形的链接。 https://public.tableau.com/profile/julien1554#!/vizhome/ManagerChallenges2014-2015/Sheet1 https://public.tableau.com/profile/julien1554#!/vizhome/ManagerChallenges2014-2015/Sheet1

All help is appreciated, thanks. 感谢所有帮助,谢谢。

You would just add another scatter plot layer to your plot. 您只需将另一个散点图图层添加到绘图中。 Here is the code that I used. 这是我使用的代码。 Hope it helps! 希望能帮助到你!

> df = as.data.frame(cbind(Overturned = c(24,1,1,16,11), total = c(49,1,8,28,27)))
> library(ggplot2)
> p <- ggplot(data = df, aes(x = Overturned, y = total)) # creates the graph
> p + geom_point(data = df, color = "gray") + # creates main scatter plot with gray points
   geom_point(data = df[1,], color = "blue") # colors A.J. Hinch's point blue

Here is the resulting graph: 这是结果图: 在此处输入图片说明

Note that I'm just using the last name because when I read your data from the clipboard it thought the first names were row labels. 请注意,我只是使用姓氏,因为当我从剪贴板中读取数据时,它以为姓氏是行标签。

Outcome$color_me <- ifelse(Outcome$manager == "Hinch", "color_me", "normal")
textdf           <- Outcome[Outcome$manager == "Hinch", ]
mycolors         <- c("color_me" = "blue", "normal" = "grey50")

ggplot(data = Outcome, aes(x = Overturned, y = total)) +
  geom_point(size = 3, aes(colour = color_me))

在此处输入图片说明

or with the manually defined color: 或使用手动定义的颜色:

ggplot(data = Outcome, aes(x = Overturned, y = total)) +
  geom_point(size = 3, aes(colour = color_me)) +
  scale_color_manual("Status", values = mycolors)

在此处输入图片说明

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

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