0

If you run the code below you will a line graph. How can I change the color of the point at x = 2 to RED and increase it's size?

In this case the on the graph the point at (.6) where x = 2 would be highlighted red and made bigger.

Here is my code:

library("ggplot2")
data<-data.frame(time= c(1,2,3), value = c(.4,.6,.7))
ggplot(data, aes( x = time,  y=value)  )   +   geom_line()   + geom_point(shape = 7,size = 1)  

Thank you!

user3022875
  • 8,598
  • 26
  • 103
  • 167

2 Answers2

0

If your dataset is small you could do this:

> library("ggplot2")
> data<-data.frame(time= c(1,2,3), value = c(.4,.6,.7),point_size=c(1,10,1),cols=c('black','red','black'))
> ggplot(data, aes( x = time,  y=value)  )   +   geom_line()   + geom_point(shape = 7,size = data$point_size, colour=data$cols)

Makes:

enter image description here

Also I would not advise calling your data frame data

Harpal
  • 12,057
  • 18
  • 61
  • 74
  • Thank you but I should have stated I need to do this dynamically choosing of the X value will NOT always be the second element (i.e. X= 2). Is there A way I can do it dynamically. See changed code. – user3022875 Apr 10 '14 at 16:07
  • Can you help with this http://stackoverflow.com/questions/22994121/ggplot-dynamic-shape-and-size-change-of-lines-on-graph – user3022875 Apr 10 '14 at 16:48
0

In addition to @Harpal's solution, you can add two more columns to your data frame where pointsize and -color is specified according to particular conditions:

df <- data.frame(time= c(1,2,3), value = c(.4,.6,.7))
# specify condition and pointsize here
df$pointsize <- ifelse(df$value==0.6, 5, 1)
# specify condition and pointcolour here
df$pointcol <- ifelse(df$value==0.6, "red", "black")
ggplot(df, aes(x=time, y=value)) + geom_line() + geom_point(shape=7, size=df$pointsize, colour=df$pointcol)

You may change ifelse(df$value==0.6, 5, 1) to meet any criteria you like, or you use a more complex approach to specifiy more conditions to be met:

df <- data.frame(time= c(1,2,3), value = c(.4,.6,.7))
df$pointsize[which(df$value<0.6)] <- 1
df$pointsize[which(df$value>0.6)] <- 8
df$pointsize[which(df$value==0.6)] <- 5
df$pointcol[which(df$value<0.6)] <- "black"
df$pointcol[which(df$value>0.6)] <- "green"
df$pointcol[which(df$value==0.6)] <- "red"
ggplot(df, aes(x=time, y=value)) + geom_line() + geom_point(shape=7, size=df$pointsize, colour=df$pointcol)
Daniel
  • 7,252
  • 6
  • 26
  • 38
  • Thank you. can you help with this http://stackoverflow.com/questions/22994121/ggplot-dynamic-shape-and-size-change-of-lines-on-graph – user3022875 Apr 10 '14 at 16:48