1

I'm still new to R and I have a problem with plotting my data which looks similar to this:

set.seed(529)
test <- data.frame(StartPos = rep(1:10, times = 10),
                   Response = c(sample(c("H", "M", "W"), 50, replace=T),
                              sample(c("M", "W"), 50, replace = T)),
                   Velocity = c(sample(c(-36, 36), 100, replace = T)))
head(test)

based on a criteria.

The data consists of 100 rows with the Start Positions from 3-5 and 10-11 ( each randomly generated 10 times (some 20 times like Start Position 3 which could exist 20 times)). Each of the Start Positions also has a answer which could be H for Hit, M for Miss or W for wrong. It's possible that there is no H for a certain Start Position. There is also a column called Velocity with the values -36 and 36 which describe the direction of the Stimlus which started at the certain StartPos (-36 to the right, 36 to the left).

I want to plot the number of Hits on each side (HitR and HitL) for each Start Postion in a barplot (for example 17 Hits out of 20 for StartPos 3, 7 of these are Hits to the right - the other 10 are Hits to the left) - It's similar to my previous post with 1 further condition: ggplot2 alternatives to fill in barplots, occurence of factor in multiple rows

I was able to count the number of Hits. But without the criterion whether it was a hit to the right or the left.

I accomplished that with the following code:

hit_counts <- test %>%
  mutate(StartPos = as.factor(StartPos)) %>% 
  filter(Response == "H") %>% 
  count(StartPos, .drop = FALSE) 

hit_counts

ggplot(hit_counts, aes(x = StartPos,y=n)) +
  geom_col()+labs(x="StartPos",y="Hitrate")

The Plot looked like this:

enter image description here

It's close to what I need, but I can't think of a way of how to divide or summarize the Hits based on it's travel Direction and then plot them.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
MaxMana
  • 31
  • 6
  • Would this help? Here is the link (https://stackoverflow.com/questions/8293547/how-to-plot-a-subset-of-a-data-frame-in-r) – Vishwas Jun 24 '19 at 20:12
  • I sadly didn't find a way on how to implement this with ggplot2. But I'll keep it in the back of my head, thanks! – MaxMana Jun 25 '19 at 15:10

2 Answers2

1

Something like this?

set.seed(529)
test <- data.frame(StartPos = rep(1:10, times = 10),
                   Response = c(sample(c("H", "M", "W"), 50, replace=T),
                             sample(c("M", "W"), 50, replace = T)),
                   Velocity = c(sample(c(-36, 36), 100, replace = T)))
head(test)

library(dplyr)
library(ggplot2)

test <- test %>% 
    mutate(Direction = case_when( # add direction 
        Velocity < 0 ~ "Right",
        Velocity > 0 ~ "Left",
        TRUE ~ "None")) %>% 
    filter(Response=="H") %>% 
    group_by(StartPos, Direction) %>% # for each combination 
    mutate(Count=n()) # count

ggplot() +
    geom_col(data=test, mapping=aes(x=StartPos, fill=Direction, y=Count)) +
    labs(x="StartPos", y="Hitrate")

Created on 2019-06-25 by the reprex package (v0.3.0)

Simon Woodward
  • 1,946
  • 1
  • 16
  • 24
0

Here is another example, I did work on it in the past.

`mydata2= subset (mydata, col_name == "HitR", select = c("col1", "col2", "col4"))`

Then you should be able to plot or even do subplots of "HitR" and "HitL" for comparison. Let me know if this helps.

Vishwas
  • 343
  • 2
  • 13