Consider a df that I would like to plot.
The exemplary df:
df
Entry A. B. C. D. Value
O60701 1 1 1 0 2.7181970
Q8WZ42 1 1 1 1 3.6679832
P60981 1 1 0 0 2.2974231
Q15047 1 0 0 0 0.5535473
Q9UER7 1 0 0 0 4.1030394
I want Entry to be on y axis and Value on x axis. Do you have any ideas how to create a plot, so that if a protein is found (==1) let us say in column A it would be a dot on a plot? Since we have four columns (A-D), there can be maximum 4 dots. Hence, I would like to be able to distinguish which dot (or any other shape) comes from which column.
Here is what I have so far:
ggplot(df, aes(x=Value, y=Entry)) +
geom_point(size=1) +
theme_ipsum()
library(tidyverse)
df %>%
pivot_longer(cols = A:D) %>%
# by default, pivot_longer creates `name` column with either A/B/C/D,
# and a `value` column holding the original 0/1 value from those columns
filter(value == 1) %>% # only plot if protein found (A/B/C/D==1)
ggplot(aes(Value, Entry, color = name)) +
geom_jitter(height = 0.1, width = 0.1) + # since you have multiple points at the same locations
hrbrthemes::theme_ipsum()
Related
I have a data frame of multiple columns. I want to create a two boxplots of the two variable "secretary" and "driver" but the result is not satisfiying as the picture shows boxplot. This is my code:
profession ve.count.descrition euse.count.description Qualitative.result
secretary 0 1 -0.5
secretary 0 2 1
driver 1 1 -1
driver 0 2 0.3
data %>%
mutate(Qualitative.result = factor(Qualitative.result)) %>%
ggplot(aes(x = Profession , fill = Qualitative.result)) +
geom_boxplot()
You should not make Qualitative.result as factor. Maybe you want something like this:
library(tidyverse)
data %>%
ggplot(aes(x = Profession, y = Qualitative.result, fill = Profession)) +
geom_boxplot()
Output:
I have an existing ggplot with geom_col and some observations from a dataframe. The dataframe looks something like :
over runs wickets
1 12 0
2 8 0
3 9 2
4 3 1
5 6 0
The geom_col represents the runs data column and now I want to represent the wickets column using geom_point in a way that the number of points represents the wickets.
I want my graph to look something like this :
As
As far as I know, we'll need to transform your data to have one row per point. This method will require dplyr version > 1.0 which allows summarize to expand the number of rows.
You can adjust the spacing of the wickets by multiplying seq(wickets), though with your sample data a spacing of 1 unit looks pretty good to me.
library(dplyr)
wicket_data = dd %>%
filter(wickets > 0) %>%
group_by(over) %>%
summarize(wicket_y = runs + seq(wickets))
ggplot(dd, aes(x = over)) +
geom_col(aes(y = runs), fill = "#A6C6FF") +
geom_point(data = wicket_data, aes(y = wicket_y), color = "firebrick4") +
theme_bw()
Using this sample data:
dd = read.table(text = "over runs wickets
1 12 0
2 8 0
3 9 2
4 3 1
5 6 0", header = T)
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:
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)
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.