Search code examples
rplotggplot2histogramdensity-plot

Flag values in plot?


I have a dot plot as below for my data that I created using the TeachingDemos package. I would like to plot some data points in a different colour. These are marked by a * below. Sitbu already answer to this part but now I would like to flag that points in density plot and hist plot?

input:

a1<- c(0.2,0.3,0.5)
a2<-c(1,0.9,0.7)
a3<-c(0.8,0.1,0.12)
a4<-c(0.4,2,0.33)
a<-cbind(a1,a2,a3,a4)
a
     a1  a2   a3   a4
[1,] 0.2 1.0* 0.80 0.40
[2,] 0.3 0.9* 0.10 2.00*
[3,] 0.5 0.7 0.12 0.33

dots(a)

enter image description here1

I want a plot like this: enter image description here2

Also, I would like to flag that points in density plot and hist plot?

density(a)

hist(a)
lines(d, col="red")

Solution

  • It seems that you are using the dots() function from the package TeachingDemos. Looking at it's source code, I can reproduce your first plot as follows:

    x <- a
    y <- as.vector(table(x))
    plot(x, y, ylab = "Count")
    

    enter image description here

    The trick is now, to pick the points that should be red from x and set their colour to red. This can be done by first creating a vector that contains "black" for each dot, and then overwrite some of the by "red":

    dot_col <- rep("black", length(a))
    red_x <- c(1.0, 0.9, 2.00)
    dot_col[match(red_x, x)] <- "red"
    plot(x, y, col = dot_col, ylab = "Count")
    

    enter image description here