0
votes

enter image description hereenter image description here

I am working on a scatterplot in RStudio and am trying to change the color of only two data points from black to red. I have tried commands like points(bradyscore[-2.0], sum[11.6], col="red") but it either comes out with an error code saying that the variables are different lengths (which they should not be as it is the coordinates for a data point), or it changes my whole plot by changing the shape of the points, the title and axis. And I also want to color two specific data points not just one. (bradyscore =x and sum=y) there is a categorical variable that is the label for each data point if that is useful in any way. Pictured is the plot that I have now, and I do not want to change anything about the plot except the color of two data points: DC(50,13.9) and Louisiana(-2.0, 11.6). There are 51 total data points.

2
provide the code you have so farReza

2 Answers

1
votes

in the points() function, just give the coordinates of the point which you want to recolor or reshape

points(c(50,-2),c(13.9,11.6),col="red")

1
votes

Problem with what you did

When isolating one element of a vector, you have to provide its position in the vector:

x <- 11:20
x[5]
# [1] 15

x[15]
# [1] NA
# -> there is no 14th element of x, which is of length 10.

And in your case the -2.0 and 11.6 are transformed into integers and used as index:

x[-2.0]
# [1] 11 13 14 15 16 17 18 19 20
# -> removed 2nd element
x[11.6]
# [1] NA
# -> kept the 11th element (which doesn't exist here)

Here I guess your x and y variables are bradyscore and sum? So you need to find the position of Louisiana in these vectors, and use this position as a subscript.


Let's start with some data similar to what you have:

library(tidyverse)
Guns_data <- tribble(~jurisdiction, ~sum, ~bradyscore,
                     "D.C.",        13.9,        50.0,
                     "Delaware",     6.2,        35.4,
                     "Florida",      5.3,         3.0,
                     "Kentucky",     4.9,        -3.5,
                     "Louisiana",   16.6,        -2.0)

Examples 1: subsetting the data frame

We can directly choose the values from jurisdiction that we'll want to color:

jurisd_to_color <- c("Louisiana", "Delaware")

With ggplot2

We can add a column that indicates which color group each jurisdiction belongs to:

Guns_data <- mutate(Guns_data,
                    my_colors = if_else(jurisdiction %in% jurisd_to_color,
                                    "to_plot_in_red",
                                    "to_plot_in_black"))

# then plot it
ggplot(Guns_data, aes(x=bradyscore,y=sum,color=my_colors)) +
  geom_point(shape=15) +
  ggrepel::geom_label_repel(aes(label=jurisdiction)) +
  scale_color_manual(values=c("red","black")) +
  theme_classic()

With base R

We again add a column with the colors to plot:

Guns_data$my_colors <- ifelse(Guns_data$jurisdiction %in% jurisd_to_color,
                          "red",
                          "black")

# Now plot the points
plot(x    = Guns_data$bradyscore,
     y    = Guns_data$sum,
     col  = Guns_data$my_colors,
     pch  = 15,
     xlab = "Brady Score",
     ylab = "Gun Deaths")

# and the labels
text(x      = Guns_data$bradyscore,
     y      = Guns_data$sum,
     labels = Guns_data$jurisdiction,
     col    = Guns_data$my_colors,
     pos=4)

Examples 2: with coordinates

Instead of adding a column to our data frame, we can plot the whole thing in black, and then indicate the coordinates of the points to overplot in red. Here, we will plot Louisiana in red, so x=-2, y=16.6.

With ggplot2

We add a second call to geom_point that will overplot on the existing points:

ggplot(Guns_data, aes(x=bradyscore,y=sum)) +
  geom_point(shape=15) +
  ggrepel::geom_label_repel(aes(label=jurisdiction)) +
  geom_point(aes(x=-2.0,y=16.6),
             color="red", shape=15) +
  theme_classic()

With base R

We can use points() to plot on top of the existing figure:

plot(x    = Guns_data$bradyscore,
     y    = Guns_data$sum,
     pch  = 15,
     xlab ="Brady Score",
     ylab ="Gun Deaths")

points(x   = -2,
       y   = 16.6,
       col = "red",
       pch = 15)

And you can do the same with text to overwrite the black text.