Hi Patricia
You are somewhat circling around solution.
Is this what you wanted?
for (i in 5:7) {
plotname = paste("Graph", names(scores)[i], sep="")
png(paste0(plotname,".png"))
p <- ggplot(scores, aes(x=scores[,i], fill=gender ))
print(p+ geom_density(alpha=.3)+xlab(names(scores)[i]))
dev.off()
}
Cheers
Petr
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Patricia Seo
> Sent: Tuesday, October 28, 2014 1:59 AM
> To: r-help at r-project.org
> Subject: [R] Loop with ggplot2 not as simple as it seems...
>
> Hi everyone,
>
> I have been battling with this problem for the past month and reading
> all that I can about it, but I just can't seem to understand what
I'm
> doing wrong. It seems easy and I can replicate others well-recorded
> attempts, but can not seem to apply this to my data.
>
> I have created a data.frame to easily illustrate my problem. I would
> like to plot columns 5:7 in my data without having to write-out-every-
> single-variable name. In this example it is only three columns but in
> my actual data set it is more like 50 columns. Here is the simplified
> data frame:
>
> sid <- c(1001:1010) #student id
> age <- c(10, 12, 14, 15, 13, 16, 14, 12, 14, 10)
> race <- race <- c("w", "b", "a",
"w", "a", "w", "b", "a",
"w", "a")
> gender <- gender <-c("M", "F", "M",
"F", "M", "F", "M", "F",
"M", "F")
> read <- rnorm(10, 100, 50)
> write <- rnorm(10, 100, 50)
> math<-rnorm(10, 100, 50)
>
> scores <- data.frame(sid, age, race, gender, read, write, math)
>
> My end goal is to produce a .png like this for every column:
>
> ggplot(scores, aes(x=scores$read, fill=scores$gender)) +
> geom_density(alpha=.3)
>
> In other words, I would like to write a loop that would produce
> separate .pngs for each column. No facet-wrap. I would like one plot
> showing the distribution of one score for male and female in one .png
> file.
>
> Failed Attempt #1 of 3: My first attempt was to melt data since I know
> ggplot2 likes melted data. I have tried melting the data using sid,
> age, race and gender as the id variable. But I?m not sure how to plot
> what I would like above with just using values. In my actual data set
> there are over 50 columns to plot (instead of the simple three in the
> example scores data). I tried creating:
>
> xobject <- subset(scores.melt, variable=read)
>
> but it gives me the error: Don't know how to automatically pick scale
> for object of type data.frame. Defaulting to continuous
> Error: Aesthetics must either be length one, or the same length as the
> dataProblems:test1
>
> Failed Attempt #2 of 3: My second attempt is below but it had similar
> problems to what others have mentioned with ggplot changing the names
> of each plot but keeping the last known data column so all the graphs
> look the same. It works in terms of titles but not data! And I have
> tried as_string() but to no avail:
>
> for (i in 5:7) {
> column_to_plot = as.character(paste("Col_", i,
sep=""))
> png(paste0("Graph", column_to_plot,".png"))
> ggplot(scores, aes(x=column_to_plot, fill=gender )) +
> geom_density(alpha=.3)
> ggsave(paste0("Graph", column_to_plot,".png"))
>
> }
>
> Failed Attempt #3 of 3: My third attempt is my most successful attempt.
> It is because I created a vector that includes the list of variables I
> would like to plot. Unfortunately, I have two problems with this
> method: (1) Don?t know how to include the corresponding title name so I
> know which plot corresponds to the score even though I do indicate the
> names of the Indexes, and (2) It is annoying to have to write the
> entire list of variables I want to plot especially if there are 50
> variable names. I know I can specify columns 5:7 (like above) but I?m
> not grasping the logics of this loop:
>
> Indexes = list()
> Indexes[[1]] = scores$read
> Indexes[[2]] = scores$write
> Indexes[[3]] = scores$math
>
> names(Indexes) = c(?Scores for Read?,
> ?Scores for Write?, ?Scores for Math?)
>
> ##### for some reason, I have to run the top half and after it has
> processed that, run the bottom half#####
>
> for(i in seq(along = Indexes)) {
> Index = Indexes[[i]]
>
> for(j in 1:length(gender)) {
> png(paste0("Graph", Index,".png"))
> ggplot(scores, aes(x=Index, fill = gender)) +
> geom_density(alpha=.3)
> ggsave(paste0("Graph", Index,".png"))
>
> }
> }
>
> Any help would be much appreciated. I know this is a frankenstein from
> previous questions and problems with loops in ggplot2, but just
> understand what I'm doing wrong would even be a huge help.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
________________________________
Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny
pouze jeho adres?t?m.
Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho
odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho
syst?mu.
Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email
jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat.
Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i
zpo?d?n?m p?enosu e-mailu.
V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?:
- vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a
to z jak?hokoliv d?vodu i bez uveden? d?vodu.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout;
Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce
s dodatkem ?i odchylkou.
- trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m
dosa?en?m shody na v?ech jej?ch n?le?itostech.
- odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost
??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn?
pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn?
osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi
?i osob? j?m zastoupen? zn?m?.
This e-mail and any documents attached to it may be confidential and are
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender.
Delete the contents of this e-mail with all attachments and its copies from your
system.
If you are not the intended recipient of this e-mail, you are not authorized to
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by
modifications of the e-mail or by delay with transfer of the email.
In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately
accept such offer; The sender of this e-mail (offer) excludes any acceptance of
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into
any contracts on behalf of the company except for cases in which he/she is
expressly authorized to do so in writing, and such authorization or power of
attorney is submitted to the recipient or the person represented by the
recipient, or the existence of such authorization is known to the recipient of
the person represented by the recipient.