Plotting two grouped variables side to side in R using ggplot - r

I Have a dataframe named mydata. Here's a sample of the relevant columns:
Backlog.Item.Type Item.Created.To.Closed.Days Item.Created.To.Finished.Days
User Story 67 84
Task 14 17
Task 9 10
Epic 105 NA
User Story 56 59
Bug 5 NA
Now, what I want to accomplish is the following: I want to take the mean for the Item.Created.To.Closed.Days column as well as for the Item.Created.To.Finished.Days column, grouped by Backlog.Item.Type, and then plot both next to eachother. To calculate the mean I use, which works:
mydata %>%
group_by(Backlog.Item.Type) %>%
summarise_at(vars(Item.Created.to.Closed.Days),
funs(mean(Item.Created.to.Closed.Days, na.rm = TRUE)))
For the plotting part, I have tried something like
mydata.long <- melt(mydata)
ggplot(mydata.long,
aes(Backlog.Item.Type, value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge")
But I can't seem to get it to work. I should also note that I only want to plot the means for Backlog.Item.Type == 'User Story' and Backlog.Item.Type == 'Task' for both columns. Represented visually, this is what I want to accomplish:
Please excuses my horrible paint skills! I don't have any preference for colors or whatnot, I just need to get it done :D Thanks in advance, I hope I have been clear enough and formulated my question in a understandable manner!

assuming that the graph you provided includes your whole data set and therefore should not be corresponding to the sample data you provided here is what you can do:
mydata=mydata %>% group_by(Backlog.Item.Type) %>% summarise(Item.Created.To.Closed.Days=
mean(Item.Created.To.Closed.Days,na.rm=T),
Item.Created.To.Finished=mean(Item.Created.To.Finished,na.rm=T))
mydata=mydata[complete.cases(mydata),]%>%melt()
ggplot(mydata,aes(x=Backlog.Item.Type,y=value,fill=variable))+geom_bar(stat = "identity", position = "dodge")

Related

How to plot a table containing counts by class in R?

How do you visulize in R this kind of table, where x is the class and y the number of occurences in the class?
I want to visualize the distribution but all I manage to do is a barplot (putting y into a vector), so it doesn't use the info of x. I can add the tags afterwards but is there a better way to proceed and directly use this kind of format.
Plus, if I have thousands of class, how can I just plot them with larger bins that the one in the table? (example here this could be plotting just two class <50 and >50).
x y
100 1954
90 106
80 700
70 27
60 861
50 32
40 5491
30 936
20 7364
10 408
You may use barplot
barplot(y~x, df)
Or in ggplot2
library(ggplot2)
ggplot(df, aes(x, y)) + geom_col()
If you need only two categories, you can create new column and then use aggregate.
df$class <- ifelse(df$x > 50, 'less than 50', 'higher than 50')
barplot(y~class, aggregate(y~class, df, sum))
Following #Ronak Shah said, you can make graph. For many classes, you may use cut. For example, split your data as >50 and <50,
df %>%
mutate(grp = cut(V1,2)) %>%
ggplot(aes(grp, V2)) + geom_col()
will make bar graph.
If your first column is a factor in your data, you might need to add as.numeric(first column) before that.

How to plot the numbers in each of 10 columns for one row in a line chart

This seems so simple. I can easily do it in Excel but I want to automate the process through R. I have installed ggplot2. Using RStudio I have read in my CSV file.
The resulting data frame has over 200 rows, each a town in New Hampshire. The first column is titled "Town" and each row below that has the text name of the town, (e.g., "Concord" or "Lancaster"). Column 2 contains a number for each town (spending per elementary school pupil) and the title of that column in the CSV file is "01/02 Elem PPE" - but it shows as "X01.02.Elem.PPE" when using View(). Column 3 has similar numbers for each town and its title in View() is "X02.03.Elem.PPE". Columns 4 through 11 are similar.
I just want to plot a line graph of the numbers in columns 2-11 for one row (one town). It will show how the spending per pupil has changed in that town over time. There must be a simple way to do this, but I can't find it.
Please help. I am a 77 year old with some programming experience 3-5 decades ago but new to R and Rstudio only yesterday.
First, I'll make some new data that mimics yours. It should have more or less the same properties.
library(glue)
library(tidyverse)
set.seed(4314)
mat <- matrix(rpois(40, 5000), ncol=10)
colnames(mat) <- glue("X{sprintf('%2.0f', 1:10)}.{sprintf('%2.0f', 2:11)}.Elem.PPE", sep="") %>%
gsub(". ", ".0", ., fixed=TRUE) %>%
gsub("X ", "X0", ., fixed=TRUE)
df <- tibble(town = c("Concord", "Lancaster", "Manchester", "Nashua"))
df <- bind_cols(df, as_tibble(mat))
Now, this is where you would start. I'm going to assume that you read your csv into an object called df. The first thing you should do to make plotting easier is to pivot the data from wide-form (one-row and 10 columns per observation) to long-form with 1 column and 10 rows per observation. I'm going to save this in an object called df2. The pivot_longer function is in the tidyr package. The first argument is the columns that you want to change from wide- to long-form, in this case, it's everything except town. Then you tell it a variable name for the column names and a variable name for the values. Then, I'm just using a couple of regular expressions to go from X01.02.Elem.PPE to 01/02 for plotting purposes.
df2 <- df %>%
pivot_longer(-town, names_to="time", values_to="val") %>%
mutate(time = gsub("X(.*)\\.Elem\\.PPE", "\\1", time),
time = gsub("\\.", "/", time))
The resulting data frame looks like this:
# # A tibble: 40 x 3
# town time val
# <chr> <chr> <int>
# 1 Concord 01/02 4965
# 2 Concord 02/03 4953
# 3 Concord 03/04 5066
# 4 Concord 04/05 5100
# 5 Concord 05/06 4979
# 6 Concord 06/07 5090
# 7 Concord 07/08 5136
# 8 Concord 08/09 5076
# 9 Concord 09/10 5079
# 10 Concord 10/11 4945
Next, we can make a plot for a single place (before we think about automation). Let's try Concord. First, we'll save the values that we want to put on the x-axis:
xlabs <- unique(df2$time)
Next, we can use ggplot() to make the plot. In the code below, we're first piping the data frame to a filter that will pull out the values for a single town. The filtered data frame is piped into the ggplot() function. Since time in the data frame is a character vector, we need to turn it into a factor and then into a numeric to make the line plot. We add the line geometry to plot the line. Then we change the x-axis labels with scale_x_continuous(). The labs() function changes the axis labels for the x- and y-axes. Finally, ggtitle() puts the title at the top of the plot. I also like theme_bw() rather than the gray background, but that's entirely a matter of personal preference. The resulting plot looks like this:
df2 %>% filter(town == "Concord") %>%
ggplot(aes(x=as.numeric(as.factor(time)), y=val)) +
geom_line() +
scale_x_continuous(breaks=1:10, labels = xlabs) +
labs(x="Time", y="Spending per Pupil") +
ggtitle("Concord") +
theme_bw()
Now, the next part you mentioned was automation - you want to do this for every row of the original data frame. We could do that as follows. First, untown grabs the unique values of town from the data. The for() loop loops from 1 to the number of values in untown. Then you can see where "Concord" was in the previous plot, we now have untown[i]. We also use ggsave() at the end and we paste together the town name and .png. This will make a different plot for each town in R's working directory.
untown <- unique(df2$town)
for(i in 1:length(untown)){
df2 %>% filter(town == untown[i]) %>%
ggplot(aes(x=as.numeric(as.factor(time)), y=val)) +
geom_line() +
scale_x_continuous(breaks=1:10, labels = xlabs) +
labs(x="Time", y="Spending per Pupil") +
ggtitle(untown[i]) +
theme_bw()
ggsave(glue("{untown[i]}.png"), width=9, height=6)
}

Bar plot in R: ggplot arguments imply differing number of rows:

I have a list object as shown below ->
> myaggregate
input$AgeAndGender input$CTR
1 Female_<18 0.030041698
2 Female_18-24 0.010918938
3 Female_25-34 0.009839806
4 Female_35-44 0.010193773
5 Female_45-54 0.009996056
6 Female_55-64 0.020024678
7 Female_65+ 0.030060728
8 Male_<18 0.028356698
9 Male_18-24 0.011031902
10 Male_25-34 0.010218562
11 Male_35-44 0.010168911
12 Male_45-54 0.010021256
13 Male_55-64 0.020191223
14 Male_65+ 0.029717747
Im trying to plot a bargraph representing the CTR levels(Y axis) for each value in AgeAndGender(X axis).
When I attempt a simple plot however I run into the following issue ->
> ggplot(data= myaggregate,aes(x=input$AgeAndGender,y=input$CTR))+geom_bar()
Error in data.frame(x = c("Male_35-44", "Female_65+", "Male_25-34", "Female_45-54", :
arguments imply differing number of rows: 3378934, 14
I'm sure I'm missing something pretty basic. Any help is appreciated!
If you are just wanting to plot the values, then you need stat="identity" like in the following example:
library(ggplot2)
AgeAndGender <- c("f1","f2","f3")
CTR <- c(.1,.15,.12)
myaggregate <- data.frame(AgeAndGender, CTR)
ggplot(data= myaggregate,aes(x=AgeAndGender, y=CTR)) + geom_bar(stat = "identity")
Which results in the following:
Looking at your comment about your data being in a list concerns me. Try making myaggregate a dataframe.
I was able to plot with something like what you are using but it's a rather weird construction. Dataframes do not generally have dollar-signs in there name because $ is an infix function in R. I read in the data with read.table and the dollar-signs get converted to periods. I put back the column names as you have them with:
names(myaggregate) <- c('input$AgeAndGender', 'input$CTR')
And then you can get a rather messy barplot with:
ggplot(data= myaggregate,aes(x=`input$AgeAndGender`,y=`input$CTR`))+ geom_bar(stat = "identity")
When you just put your code in, the unquoted names get interpreted as x being the "AgeAndGender"-clumn in the input dataframe. If you only use ordinary quotes rather than backticks you do not succeed.

Why does stacked bar plot change when add facet in r ggplot2

I have data that are in several groups, and I want to display them in a faceted stacked bar chart. The data show responses to a survey question. When I look at them in the dataframe, they make sense, and when I plot them (without faceting), they make sense.
However the data appear to change when they are faceted. I have never had this problem before. I was able to re-create a change (not the exact same change) with some dummy data.
myDF <- data.frame(rep(c('aa','ab','ac'), each = 9),
rep(c('x','y','z'),times = 9),
rep(c("yes", "no", "maybe"), each=3, times=3),
sample(50:200, 27, replace=FALSE))
colnames(myDF) <- c('place','program','response','number')
library(dplyr)
myDF2 <- myDF %>%
group_by(place,program) %>%
mutate(pct=(100*number)/sum(number))
The data in myDF are basically a count of responses to a question. The myDF2 only creates a percent of respondents with any particular response within each place and program.
library(ggplot2)
my.plot <-ggplot(myDF2,
aes(x=place, y=pct)) +
geom_bar(aes(fill=myDF$response),stat="identity")
my.plot.facet <-ggplot(myDF2,
aes(x=place, y=pct)) +
geom_bar(aes(fill=myDF$response),stat="identity")+
facet_wrap(~program)
I am hoping to see a plot that shows the proper "pct" for each "response" within each "program" and "place". However, my.plot.facet shows only one "response" per place.
The data are not like that. For example, head(myDF2) shows that program 'aa' in place 'x' has both yes and no.
> head(myDF2)
Source: local data frame [6 x 5]
Groups: place, program
place program response number pct
1 aa x yes 69 18.35106
2 aa y yes 95 25.81522
3 aa z yes 192 41.64859
4 aa x no 129 34.30851
5 aa y no 188 51.08696
6 aa z no 162 35.14100
It turns out that ORDER matters here. The myDF2 is not a data frame anymore. It is a dplyr object. That means that ggplot2 is really struggling.
If the data need to be faceted by program, 'program' needs to be first called in the group_by()
Note that this is true here by looking at the inverse plot faceting.
my.plot.facet2 <-ggplot(myDF2,
aes(x=program, y=pct)) +
geom_bar(aes(fill=myDF2$response),stat="identity")+
facet_wrap(~place)
produces:

How do you plot a histogram of the terms that occur n or more times?

I have a list of words coming straight from file, one per line, that I import with read.csv which produces a data.frame. What I need to do is to compute and plot the numbers of occurences of each of these words. That, I can do easily, but the problem is that I have several hundreds of words, most of which occur just once or twice in the list, so I'm not interested in them.
EDIT https://gist.github.com/anonymous/404a321840936bf15dd2#file-wordlist-csv here is a sample wordlist that you can use to try. It isn't the same I used, I can't share that as it's actual data from actual experiments and I'm not allowed to share it. For all intents and purposes, this list is comparable.
A "simple"
df <- data.frame(table(words$word))
df[df$Freq > 2, ]
does the trick, I now have a list of the words that occur more than twice, as well as a hard headache as to why I have to go from a data.frame to an array and back to a data.frame just to do that, let alone the fact that I have to repeat the name of the data.frame in the actual selection string. Beats me completely.
The problem is that now the filtered data.frame is useless for charting. Suppose this is what I get after filtering
Var1 Freq
6 aspect 3
24 colour 7
41 differ 18
55 featur 7
58 function 19
81 look 4
82 make 3
85 mean 7
95 opposit 14
108 properti 3
109 purpos 6
112 relat 3
116 rhythm 4
118 shape 6
120 similar 5
123 sound 3
obviously if I just do a
plot(df[df$Freq > 2, ])
I get this
which obviously (obviously?) has all the original terms on the x axis, while the y axis only shows the filtered values. So the next logical step is to try and force R's hand
plot(x=df[df$Freq > 2, ]$Var1, y=df[df$Freq > 2, ]$Freq)
But clearly R knows best and already did that, because I get the exact same result. Using ggplot2 things get a little better
qplot(x=df[df$Freq > 2, ]$Var1, y=df[df$Freq > 2, ]$Freq)
(yay for consistency) but I'd like that to show an actual histograms, y'know, with bars, like the ones they teach in sixth grade, so if I ask that
qplot(x=df[df$Freq > 2, ]$Var1, y=df[df$Freq > 2, ]$Freq) + geom_bar()
I get
Error : Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Defunct; last used in version 0.9.2)
so let us try the last suggestion, shall we?
qplot(df[df$Freq > 2, ]$Var1, stat='identity') + geom_bar()
fair enough, but there are my bars? So, back to basics
qplot(words$word) + geom_bar() # even if geom_bar() is probably unnecessary this time
gives me this
Am I crazy or [substitute a long list of ramblings and complaints about R]?
I generate some random data
set.seed(1)
df <- data.frame(Var1 = letters, Freq = sample(1: 8, 26, T))
Then I use dplyr::filter because it is very fast and easy.
library(ggplot2); library(dplyr)
qplot(data = filter(df, Freq > 2), Var1, Freq, geom= "bar", stat = "identity")
First of all, at least with plot(), there.s no reason to force a data.frame. plot() understands table objects. You can do
plot(table(words$words))
# or
plot(table(words$words), type="p")
# or
barplot(table(words$words))
We can use Filter to filter rows, unfortunately that drops the table class. But we can add that back on with as.table. This looks like
plot(as.table(Filter(function(x) x>2, table(words$words))), type="p")

Resources