how to add values to points

29 views
Skip to first unread message

lily li

unread,
Mar 23, 2015, 12:38:29 PM3/23/15
to ggp...@googlegroups.com
Hi ggplot users,

For such a dataframe, how to add day values (the 'day' column) beside each point? Thanks.
plotdata = ggplot()+geom_point(data=df,aes(x=date,y=temp,color='red))+xlab('Time')+ylab('Temperature')
show(plotdata)

year    month    day    temp    precip       date
2008      1           1       -10         0.0    2008-01-01
2008      1           2         5          NA    2008-01-02
2008      1           3         2          0.1    2008-01-03
2008      1           4         1          0.2    2008-01-04
2008      1           5         2          0.1    2008-01-05
2008      1           6        -3          0.2    2008-01-06
2008      1           7        -8          NA    2008-01-07

Dennis Murphy

unread,
Mar 23, 2015, 12:53:51 PM3/23/15
to lily li, ggp...@googlegroups.com
Hi:

Here's one way:

DF <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
year month day temp precip date
2008 1 1 -10 0.0 2008-01-01
2008 1 2 5 NA 2008-01-02
2008 1 3 2 0.1 2008-01-03
2008 1 4 1 0.2 2008-01-04
2008 1 5 2 0.1 2008-01-05
2008 1 6 -3 0.2 2008-01-06
2008 1 7 -8 NA 2008-01-07")

DF$date <- as.Date(DF$date)

# Find the day of the week and produce an abbreviated day label
library(lubridate)
DF$dow <- wday(DF$date, label = TRUE)

# Use geom_text to position the text
library(ggplot2)
ggplot(DF, aes(x = day, y = temp)) +
geom_point(color = "darkorange") +
geom_text(aes(x = day + 0.1, label = dow), size = 3, hjust = 0)

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

lily li

unread,
Mar 23, 2015, 1:02:51 PM3/23/15
to Dennis Murphy, ggp...@googlegroups.com
Thanks, Dennis. What does hjust mean? How to put labels to different sides of the plot?
Reply all
Reply to author
Forward
0 new messages