[R] qqplot: bar outline colour?

Ulrik Stervbo ulrik.stervbo at gmail.com
Mon Dec 19 14:58:25 CET 2016


Hi Dagmar,

I hope this code below does what you want.

I use two data.frames. One is for the tiles and one is for the lines to
show changes in state. The 'reduce_entries' function is the heart of things
and can probably be improved.

Ulrik

library(ggplot2)
library(lubridate)
library(dplyr)
library(purrr)

exdatframe <- data.frame(Name=c("Ernie","Ernie","Ernie",
"Leon","Leon","Leon"),

recordedTime=c("03.01.2011","04.01.2011","05.01.2011","04.01.2011","05.01.2011","06.01.2011"),
  knownstate =c("breeding","moulting","moulting","breeding","breeding",NA))

exdatframe <- exdatframe %>%
  # Convert to date
  mutate(recordedTime = dmy(recordedTime))

reduce_entries <- function(.data){
  for(i in seq_along(1:nrow(.data))){
    this_row <- .data[i, ]
    next_row <- .data[i + 1, ]

    if(!is.na(next_row$knownstate)){
      if(this_row$knownstate == next_row$knownstate){
        this_row$recordedTime <- next_row$recordedTime
      }
    }

   .data[i, ] <- this_row
  }
  .data %>% distinct()
}

state_delimiter <- exdatframe %>%
  split(.$Name) %>%
  map_df(reduce_entries) %>%
  # Get the numerical values for the Names
  mutate(name.y = group_indices_(., .dots = "Name")) %>%
  mutate(name.y.start = name.y - 0.4, name.y.end = name.y + 0.4)

exdatframe %>%
  ggplot() +
  aes(x = recordedTime, y = Name, fill = knownstate) +
  geom_tile() +
  geom_segment(aes(x = recordedTime + 0.5, xend = recordedTime + 0.5,
    y = name.y.start, yend = name.y.end), colour = "black", size = 2,
    data = state_delimiter)

On Mon, 19 Dec 2016 at 12:09 Dagmar <Ramgad82 at gmx.net> wrote:

> Dear all,
>
> Apearantly noone in this great mailing list could help me with my
> problem a couple of days ago. As I still struggle with part of the
> problem I try to explain my problem differently:
>
> # This is my example data:
>      exdatframe <- data.frame(Name=c("Ernie","Ernie","Ernie",
> "Leon","Leon","Leon"),
> recordedTime=c("03.01.2011","04.01.2011","05.01.2011",
> "04.01.2011","05.01.2011","06.01.2011"),
>                       knownstate =c("breeding","moulting","moulting",
>                               "breeding","breeding",NA))
>      exdatframe
>      str(exdatframe)
>      exdatframeT <- as.POSIXct
> (strptime(as.character(exdatframe$recordedTime),"%d.%m.%Y"))
>      exdatframeT
>      exdatframe2 <- cbind(exdatframe, exdatframeT)
>      exdatframe2$recordedTime <-NULL
>      exdatframe2
>      str(exdatframe2)
>
> # I made this plot:
>
> n <- ggplot(exdatframe2, aes(exdatframeT,Name)) +
> geom_tile(aes(fill=knownstate), colour="black", height=0.5)
> n
>
> # my question: How do I get a bar outline colour around all of breeding
> that is not interupted each day (I only want vertical bars between
> different "knownstates")
>
> # Help would be terribly much appreciated!!!!
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list