Hi, I cannot figure out how to draw a certain plot: could someone help me out? I have this data.frame from a survey my.data that looks like something like this: col1 col2 col3 col4 1 5 5 4 5 2 3 5 3 1 3 2 3 4 5 4 3 1 1 2 5 5 5 4 5 6 4 2 5 5 .... Each row represents a single questionnaire with someone giving his agreement/disagreement with a statement (each column is a statement) that is coded from 1 to 5. I need to draw a barplot giving a visual representation showing differences between the five columns: Each bar should represent a single column, and should be divided into 5 sections, the thickness of each depending on the number of respondents who choose that particular answer. How do I do that? All I have managed to do so far is to produce a barplot of a single column, and that - only with bars side by side... -- Donatas Glodenis http://dg.lapas.info
Dear members I have imported an SPSS data file using Hmisc. So label(mydata[[1]]) gives me the first variable label Just wondering how I can access all the variable labels as a vector? Something like label(mydata[[1:3]]) but that doesn't work. Thanks in advance Steve Powell proMENTE social research research | evaluation | training & consulting
Hi,>Each row represents a single questionnaire with someone giving his >agreement/disagreement with a statement (each column is a statement) that is >coded from 1 to 5.Maybe I mixed up columns and rows (because I find only 4 columns in you example) but it would go something like this with ggplot2: library("reshape") library("ggplot2") my.data$id=1:nrow(my.data) resp <- melt(my.data,id=5) p <- ggplot(resp, aes(x=factor(variable), fill=factor(value))) + geom_bar(position="fill") http://had.co.nz/ggplot2/position_fill.html best wishes ido
Dear Donatas, I think that I understand what you are looking for. I've created it in the most basic way (for two columns, no looping, no additional packages). Please note that this only works if every value is represented in every column. If this is not the case, some additional checks need to be implemented (i.e. converting the data to a factor). If you are working with many columns, it will probably help to place this bit of code in a loop (I will do this for you, if you want to). So, the core of the code looks like: col1 <- c(5, 3, 2, 3, 5, 4, 1) col2 <- c(5, 5, 3, 1, 5, 2, 4) plot.mat <- matrix(data=NA, nrow=5, ncol=2) plot.mat[,1] <- table(col1) plot.mat[,2] <- table(col2) barplot(plot.mat, beside=FALSE) Obviously, this can be elaborated upon. Hope this helps, Rense Nieuwenhuis On Jul 16, 2007, at 9:06 , Donatas G. wrote:> Hi, > > I cannot figure out how to draw a certain plot: could someone help > me out? > > I have this data.frame from a survey > my.data > > that looks like something like this: > > col1 col2 col3 col4 > 1 5 5 4 5 > 2 3 5 3 1 > 3 2 3 4 5 > 4 3 1 1 2 > 5 5 5 4 5 > 6 4 2 5 5 > .... > > > Each row represents a single questionnaire with someone giving his > agreement/disagreement with a statement (each column is a > statement) that is > coded from 1 to 5. > > I need to draw a barplot giving a visual representation showing > differences > between the five columns: Each bar should represent a single > column, and > should be divided into 5 sections, the thickness of each depending > on the > number of respondents who choose that particular answer. > > How do I do that? All I have managed to do so far is to produce a > barplot of a > single column, and that - only with bars side by side... > > -- > Donatas Glodenis > http://dg.lapas.info > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
On 7/16/07, Donatas G. <dgvirtual at akl.lt> wrote:> Hi, > > I cannot figure out how to draw a certain plot: could someone help me out? > > I have this data.frame from a survey > my.data > > that looks like something like this: > > col1 col2 col3 col4 > 1 5 5 4 5 > 2 3 5 3 1 > 3 2 3 4 5 > 4 3 1 1 2 > 5 5 5 4 5 > 6 4 2 5 5 > .... > > > Each row represents a single questionnaire with someone giving his > agreement/disagreement with a statement (each column is a statement) that is > coded from 1 to 5. > > I need to draw a barplot giving a visual representation showing differences > between the five columns: Each bar should represent a single column, and > should be divided into 5 sections, the thickness of each depending on the > number of respondents who choose that particular answer. > > How do I do that? All I have managed to do so far is to produce a barplot of a > single column, and that - only with bars side by side...One way would be the use the ggplot2 and reshape packages: library(ggplot2) df <- as.data.frame(matrix(sample(1:5, 100, rep=T), ncol=5)) dfm <- melt(df, m=1:5) qplot(variable, data=dfm, geom="bar", fill=factor(value)) qplot(variable, data=dfm, geom="bar", fill=factor(value), position="dodge") qplot(variable, data=dfm, geom="bar", fill=factor(value), facets = . ~ value) Hadley
I did not have much success with ggplot2 and reshape libraries, so finally I managed to work out the graph in a rather complicated way. The only thing I cannot figure out is, how do I place the legend outside the barplot column area? attach(tolerancija.data) ateist = table(G09_01) dvasin = table(G09_02) gamtos = table(G09_03) kr_klm = table(G09_04) musulm = table(G09_05) na_kri = table(G09_06) pagoni = table(G09_07) satani = table(G09_08) ryt?_k = table(G09_08) satani = table(G09_09) tradic = table(G09_10) eilutes=cbind(ateist,dvasin,gamtos,krik??,musulm,kr_klm,na_kri,pagoni,ryt?_k,satani,tradic) barplot(prop.table(eilutes,2),col=c("red3","red2","cyan","green2","green3"),legend.text=c("visi?kai sutinku","nesutinku","abejoju","sutinku","visi?kai sutinku"),main="My title") dettach(tolerancija.data) However, this is a lot of writing. How do I make into a function to be reusable? Donatas On Jul 16, 2007, at 9:06 , Donatas G. wrote:> Hi, > > I cannot figure out how to draw a certain plot: could someone help ? > me out? > > I have this data.frame from a survey > my.data > > that looks like something like this: > > ? ?col1 ?col2 ?col3 ?col4 > 1 ? ? 5 ? ? 5 ? ? 4 ? ? 5 > 2 ? ? 3 ? ? 5 ? ? 3 ? ? 1 > 3 ? ? 2 ? ? 3 ? ? 4 ? ? 5 > 4 ? ? 3 ? ? 1 ? ? 1 ? ? 2 > 5 ? ? 5 ? ? 5 ? ? 4 ? ? 5 > 6 ? ? 4 ? ? 2 ? ? 5 ? ? 5 > .... > > > Each row represents a single questionnaire with someone giving his > agreement/disagreement with a statement (each column is a ? > statement) that is > coded from 1 to 5. > > I need to draw a barplot giving a visual representation showing ? > differences > between the five columns: Each bar should represent a single ? > column, and > should be divided into 5 sections, the thickness of each depending ? > on the > number of respondents who choose that particular answer. > > How do I do that? All I have managed to do so far is to produce a ? > barplot of a > single column, and that - only with bars side by side...-- Donatas Glodenis http://dg.lapas.info