data order and color in ggplot - r

I have assets by manager in a data frame
Date C B A E D
2011-06-30 20449251 2011906 0 0 0
2011-09-30 20766092 1754940 0 0 0
2011-12-31 15242138 1921684 0 0 0
2012-03-31 15811841 2186571 0 0 0
2012-06-30 16221813 2026042 2423039 2419517 0
2012-09-30 16155686 2261729 2563734 1160693 0
2012-12-31 16297839 2231341 2592015 1151989 0
2013-03-31 14627046 2441132 2769681 1249464 0
2013-06-30 14186185 2763985 2615053 1260893 0
2013-09-30 14039954 2780167 2698988 1264244 0
2013-12-31 13832117 3081687 2962113 1318903 0
2014-03-31 14177177 3133202 3077684 1353243 0
2014-06-30 14503900 3235089 3196623 1415319 0
2014-09-30 12561057 3227862 3048216 1413446 2073068
I then melt and plot to get a stacked area graph
library('ggplot2')
library('reshape2')
colorscheme = scale_fill_brewer(type="qual",palette = 2)
df = melt(data,id.var="Date",variable.name="Manager")
df[,3] = as.numeric(df[,3])
#Stacked Area
layout(c(1,1))
p = ggplot(df,aes(x=Date,y=value,group=Manager,fill=Manager))+
geom_area(position="fill") + colorscheme
print(p)
and this works great:
Now I want a pie chart of the last row (i.e, current date)
df1 = data[nrow(data),-1]
df1 = as.data.frame(t(df1))
colnames(df1) = "AUM"
p = ggplot(df1,aes(x=1,y=df1$AUM,fill=rownames(df1))) +
geom_bar(stat="identity") + colorscheme + coord_polar(theta="y")
plot(p)
and I get the following:
Ignoring the formatting, my question is about the color selection. The colors don't match by manager. Manager A color in the area graph is now the color for Manager C. I realize it is because the pie chart is sorted by Manager name where as the Manager order in data isn't sorted.
I don't have control of how I receive data. Is there way to reorder data and/or df (data melted) so that the first graph is in manager order? Or change the way data is sent to the pie chart?
Thanks,

Rather than messing around with factor levels, wouldn't it just be easier to subset df by the Date from the last row in data??
ggplot(df[df$Date==tail(data,1)$Date,],aes(x=1,y=value,fill=Manager)) +
geom_bar(stat="identity") + colorscheme + coord_polar(theta="y")

Related

ggplot2 - color intensity (Transparent to deep) for single color in a multi color plot

Here is the data
Then assign above data under a variable "mydata"
mydata <- mydata
library(ggplot2)
p<- ggplot(mydata,aes(x=tSNE_1,y=tSNE_2)) +
geom_point(aes(colour=factor(cluster), shape = variable))
print(p)
Then we the the following output
ggplot image
Now our data structure is
> head(mydata)
tSNE_1 tSNE_2 cluster variable value
1 -1.0794380 -15.2732678 3 Pde1a 1
2 2.5828641 -14.8813390 3 Pde1a 0
3 -6.9095978 -9.5625352 1 Pde1a 0
4 5.4793466 7.3691444 0 Pde1a 3
5 -10.6781109 -11.5297445 1 Pde1a 0
6 0.5156696 0.5353185 1 Pde1a 0
Now the above image has different color based on "cluster".
But I want to plot same image as above but with cluster color intensity (Transparent [min] to Deep [max] color) based on "value". How can i do it ?
Thanks in advance !!

how to make the boxplots with dot points and labels?

I have a dataframe as below
G1 G2 G3 G4 group
S_1 0 269.067 0.0817233 243.22 N
S_2 0 244.785 0.0451406 182.981 N
S_3 0 343.667 0.0311259 351.329 N
S_4 0 436.447 0.0514887 371.236 N
S_5 0 324.709 0 293.31 N
S_6 0 340.246 0.0951976 393.162 N
S_7 0 382.889 0.0440337 335.208 N
S_8 0 368.021 0.0192622 326.387 N
S_9 0 267.539 0.077784 225.289 T
S_10 0 245.879 0.368655 232.701 T
S_11 0 17.764 0 266.495 T
S_12 0 326.096 0.0455578 245.6 T
S_13 0 271.402 0.0368059 229.931 T
S_14 0 267.377 0 248.764 T
S_15 0 210.895 0.0616382 257.417 T
S_16 0.0401525 183.518 0.0931699 245.762 T
S_17 0 221.535 0.219924 203.275 T
Now I want to make a multiboxplot with all the 4 genes in columns. The first 8 rows are for normal samples an rest 9 rows are tumor samples so for each gene I should be able to make 2 box plots with labels of tissues. I am able to make individual boxplots but how should I put all the 4 genes in one plot and also label the tissue for each boxplots and use the stripchart points. Is there a easy way to do it? I can only make individual plots using the row and column names but cannot mark the labels based on column groups in the plot and also plot the points with the stripchart. Any help will be appreciated. Thanks
with facet_wrap:
head(df)
G1 G2 G3 G4 group
S_1 0 269.067 0.0817233 243.220 N
S_2 0 244.785 0.0451406 182.981 N
S_3 0 343.667 0.0311259 351.329 N
S_4 0 436.447 0.0514887 371.236 N
S_5 0 324.709 0.0000000 293.310 N
S_6 0 340.246 0.0951976 393.162 N
library(reshape2)
df <- melt(df)
library(ggplot2)
ggplot(df, aes(x = variable,y = value, group=group, col=group)) +
facet_wrap(~variable, scales = 'free') + geom_boxplot()
Not sure what you mean with stripchart points, I assumed you wanted to visualize the actual points overlaid on the boxplots. Would the following suffice?
library(ggplot2)
library(dplyr)
library(reshape2)
melt(df) %>%
ggplot(aes(x = variable, y = value, col = group)) +
geom_boxplot() +
geom_jitter()
Where df is the above data frame. Result:

using ggplot2 to show multiple number columns in barplot?

Would anybody please help using ggplot2 in R, to show a barplot, where i need to show columns (first, second, third, fourth, fifth) on x axis and their values on y-axis ? without showing the column "uname".
> head(golQ1Grades)
qname uname first second third fourth fifth
1 onlinelernen_quiz_1 xxx 100 0 0 0 0
2 onlinelernen_quiz_2 xxxx 100 0 0 0 0
3 onlinelernen_quiz_4 xxxx 42 71 0 0 0
4 onlinelernen_quiz_7 xxxx 85 100 0 0 0
5 onlinelernen_quiz_1 xxx 85 100 0 0 0
6 onlinelernen_quiz_3 xxxx 71 0 0 0 0
Thanks for the advanced help.
It is my guess that you would like to display the mean value on the Y-axis.
library(ggplot2)
Data
dat<-data.frame(c(100,100,42,85,85,71), c(0,0,71,100,100,0), c(0,0,0,0,0,0), c(0,0,0,0,0,0), c(0,0,0,0,0,0))
names(dat)<-NULL
Compute mean and get new data
v1<-apply(dat, 2, mean)
nv1<-c("first","second","third", "fourth","fifth")
ndat<-data.frame(nv1, v1)
Plot
p <- ggplot(ndat, aes(factor(nv1), v1))
p + geom_bar(stat="identity")
I think the better option is dplyr and tidyr.E.g. (I change data.frame a little)
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data.frame(qname = letters[1:10],
first = seq(1,10,1),
second = seq(10,100,10),
third = seq(2,20,2))
And then use gather feature:
df <- df %>%
gather(variable, value, -qname)
in your case it will be
df <- golQ1Grades %>%
gather(variable,value, -qname, -uname)
Futhermore, instead of computing average value it is also extremely helpful facet_grid:
ggplot(df, aes(factor(qname),value))+
geom_bar(stat = "identity")+
facet_grid(.~variable)

adjust y-axis for missings in ggplot2?

First some toy data:
df = read.table(text =
"id year value sex
1 2000 0 1
1 2001 1 0
1 2002 0 1
1 2003 0 0
2 2000 0 0
2 2002 0 0
2 2003 1 0
3 2002 0 1
4 2000 0 0
4 2001 0 1
4 2002 1 0
4 2003 0 1 ", sep = "", header = TRUE)
When I want to visualize year by id for sex==1, I do
df2 <- df[df$sex==1,]
p <- ggplot(df2, aes(y=id))
p <- p + geom_point(aes(x=year))
p
How can I hide observation 2 from the graph so that the distance between each remaining id's is the same? Is there a general way how to adjust the distance between two ticks on the y-axis when my breaks are id?
Does the solution also works when using facets?
p <- ggplot(df, aes(y=id))
p <- p + geom_point(aes(x=year))
p <- p + facet_grid(sex ~.)
Edited based on OP's clarification
Create individual plots and use the gridExtra package.
I am not sure if this is what you are looking for, but the use of reorder() should help.
Just to test it out, I changed the "id" value of 4 to be 7 in your toy dataframe.
To drop levels in individual plots, you can create 2 plots and then place them side by side.
df2 <- df[df$sex==1,]
p1 <- ggplot(df2, aes(y=(reorder(id, id))))
p1 <- p1 + geom_point(aes(x=year))
p1
df3 <- df[df$sex==0,]
p2 <- ggplot(df3, aes(y=(reorder(id, id))))
p2 <- p2 + geom_point(aes(x=year))
If you notice, the id's without data are dropped. For example the following doesn't have id=2.
Now, you can use the gridExtra package which is meant for this purpose, to print out both the plots p1 and p2.
require(gridExtra)
grid.arrange(p1, p2, ncol=2)
facet_grid() includes all levels by design
Using facet_grid directly won't work, but this is by design. Facet_grip has the drop=TRUE by default. Notice that you are not seeing id's=5 or 6. If an id appears in any one panel, it is included in all the other panels to facilitate comparison.
p <- ggplot(df, aes(y=reorder(id, id)))
p <- p + geom_point(aes(x=year))
p <- p + facet_grid(sex ~.)
p

Plot many data sets (from csv) on single graph

Following this example:
http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
See the graph titled "Fitted growth curve per diet", I want to do the same thing but with a set of data that is in a CSV file such as (values are in µs, except for column "N"):
$ head RandomArray25PercentDup.csv
N SystemSort QuickSort RandomizedQuickSort TopDownMergeSort BottomUpMergeSort SelectionSort InsertionSort BubbleSort
4 0 1 0 1 0 1 0 0
5 0 0 0 1 1 0 1 0
6 0 0 0 1 1 0 0 0
7 0 0 0 0 1 0 0 0
8 0 0 1 0 1 0 1 1
...
I've tried this so far:
library(ggplot2)
library(reshape2)
data <- read.table("RandomArray25PercentDup.csv",
sep="\t",
header=TRUE)
data.m <- melt(data, id.vars = 1)
ggplot(data.m, aes(data, value, colour=variable)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Random array with ~25% duplicate values")
My background in R is very limited, and I'm trying to learn using various ressources.
I have about 800'000 rows worth of data, with 20 repetitions in the measurement of each N (the reason why I want to see the scatter in transparent with a fitting curve for each algorithm).
Replacing this
data.m <- melt(data, id.vars = 1)
with
data.m <- melt(data, id.vars = "N")
and then
ggplot(data.m, aes(data, value, colour=variable)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Random array with ~25% duplicate values")
with
ggplot(data.m, aes(N, value, colour=variable)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Random array with ~25% duplicate values")
should do the trick. First replacement isn't really necessary, but it's always preferable to use variable names in case the order of the columns change. The first argument in aes is mapped to the x-axis. data is not a column so it can't be mapped.

Resources