0

EDIT: THIS is what I want my bar plot to look like – I managed to do it on JMP, and I'd like help doing it in R. Thanks!

I'm trying to create a plot on R. There are 2 variables (columns) of interest: "TrialType" and "SMTscenes.RESP" — TrialType's values are either "Old" or "New", whereas SMT~'s values are either "1", "2", "3", "4", or "r". That's right, that "r" isn't a 5, which makes this a bit frustrating.

hist(c(df$TrialType, df$SMTscenes.RESP))

is what I've tried so far, and that gives me this histogram, which does not display the difference between "Old" and "New" – or if it does, it is not all that clear to me.

At others' suggestions (previously), I've done:

table(c(df$SMTscenes.RESP, df$TrialType))

which outputs:

1 2 3 4 5 80 150 25 17 16

And now the previous histogram's form makes sense – but that's not what I'm looking for.

Let me know if there is other info/data I can provide. If so, let me know how to upload a .csv file.

Thanks in advance!

José
  • 3
  • 4
  • I believe I have told you already. What you show as expected result **IS NOT** a histogram. It's a barplot. Please study [this FAQ](http://stackoverflow.com/a/5963610/1412059) and then improve your question. – Roland Feb 16 '16 at 16:28
  • Okay, it's a bar plot. Can you tell me how your correction helped me? Even if I do the barplot command, the output is virtually the same, except the columns are not touching. – José Feb 16 '16 at 16:34
  • 1
    The correction helps you researching the problem. Using "histogram" as a search phrase won't help you finding a solution. If you had provided a reproducible example (follow the link I gave above) I would have answered your other (deleted) question days ago. – Roland Feb 16 '16 at 16:39
  • Check this question: http://stackoverflow.com/questions/8901330/multiple-histograms-with-ggplot2-position – Mutador Feb 16 '16 at 16:49
  • 1
    I think you can do with `ggplot2` package. Maybe `geom_bar` with `position = "dodge"`. Go to this page and find the one you are looking for: (http://docs.ggplot2.org/current/geom_bar.html) – Kota Mori Feb 16 '16 at 16:49
  • @Mutador That question won't help them. They don't want a histogram, they want a facetted barplot (which is easy enough to create with ggplot2). – Roland Feb 16 '16 at 16:51

2 Answers2

1

Here is a good starting point. You just need to customize the appearance now:

scene1 <- c(rep(x = c("new"), 10), rep(x = c("old"), 2))
scene2 <- c(rep(x = c("new"), 60), rep(x = c("old"), 20))
scene3 <- c(rep(x = c("new"), 5), rep(x = c("old"), 20))
scene4 <- c(rep(x = c("new"), 2), rep(x = c("old"), 18))
sceneR <- c(rep(x = c("new"), 0), rep(x = c("old"), 18))

TrialType <- c(
  rep("1", length(scene1)), 
  rep("2", length(scene2)), 
  rep("3", length(scene3)), 
  rep("4", length(scene4)), 
  rep("r", length(sceneR)))

SMTscenes.RESP <- c(
  scene1,
  scene2,
  scene3,
  scene4,
  sceneR
)

df <- data.frame(TrialType, SMTscenes.RESP)

library(ggplot2)

ggplot(data = df, aes(SMTscenes.RESP)) +
  geom_bar() +
  facet_grid(. ~ TrialType)

Plot

Icaro Bombonato
  • 3,742
  • 1
  • 17
  • 12
  • Thank you so much – this is exactly what I was looking for. I apologize to everyone else for my question's poor phrasing. – José Feb 16 '16 at 17:14
0

First off, you really need to improve your question.

Then, you can achieve what you want with the help of two packages: data.table and ggplot2:

# create dummy data (next time, provide some, please!)
set.seed(1)
a <- data.frame(scenes= sample(c("1","2","3","4","r"),20,TRUE),
                type=sample(c("New","Old"),20, TRUE))

# load packages
library(data.table)
library(ggplot2)

# convert data frame to data table
setDT(a)

# generate counts:
b <- a[, .N, by=.(scenes, type)]

# generate plot:
ggplot(b, aes(x=type, y=N))+
     geom_bar(stat = "identity")+
     facet_wrap(facets = "scenes")

.N in the data table call creates a new variable with the counts. by= defines the groupings you want.

PavoDive
  • 6,322
  • 2
  • 29
  • 55
  • This answer is also great – and you're right, I should've improved my question. I'll learn from my mistake. – José Feb 16 '16 at 17:15