I have a list of data frames,
>head(df.list.xyg[["archae.list"]])
motif obs pred prop stat pval stdres
AAB 1189 760.1757 0.05556028 11811.94 0 16.00425
CDD 1058 249.7147 0.01825133 11811.94 0 51.62291
DDE 771 415.1314 0.03034143 11811.94 0 17.73730
FBB 544 226.3529 0.01654385 11811.94 0 21.28994
>head(df.list.xyg[["eukaryote.list"]])
motif obs pred prop stat pval stdres
ABG 82015 48922.33 0.08773749 321891.7 0 156.64562
GBC 51601 64768.42 0.11615591 321891.7 0 -55.03402
AGG 41922 30136.56 0.05404701 321891.7 0 69.80141
CGG 25545 14757.24 0.02646569 321891.7 0 90.00215
BTT 15795 12433.58 0.02229843 321891.7 0 30.48747
I would like to
barplot motif versus stdres such that the plots are displayed in single column but two rows, and
label each plot corresponding to part of the file name "archae", eukaryote and so on.
After searching around, the following code snippet does the job but only partly. It only plots the last dataset. I assume it is "overwriting" the earlier dataset. How do I fix this? Can this be achieved by face_wrap or facet_grid?
myplots<-lapply(df.list.xyg,
function(x)
p<-ggplot(x,aes(x=motif,y=stdres)) +
geom_bar(stat="identity",width=0.5, color="blue", fill="gray")
)
print (myplots)
If you just want the plots to appear together in a single plotting window, you can use facets. You can bind the rows of your data frames together with dplyr::bind_rows, which will create an id column to label which data frame each row belongs to. We facet by this ID variable.
library(ggplot2)
ggplot(dplyr::bind_rows(df.list.xyg, .id = "Kingdom"), aes(motif, stdres)) +
geom_col(width = 0.5, fill = "deepskyblue4") +
geom_hline(yintercept = 0, color = "gray75") +
facet_grid(.~Kingdom) +
theme_bw(base_size = 16)
If you have lots of different data frames in your list, facet_grid may be better than facet_wrap.
Depending on how you wish to present the data, you may prefer to save individual plots to files using ggsave inside your lapply
Reproducible data taken from question
df.list.xyg <- list(archae = structure(list(motif = c("AAB",
"CDD", "DDE", "FBB"), obs = c(1189L, 1058L, 771L, 544L),
pred = c(760.1757, 249.7147,
415.1314, 226.3529), prop = c(0.05556028, 0.01825133, 0.03034143,
0.01654385), stat = c(11811.94, 11811.94, 11811.94, 11811.94),
pval = c(0L, 0L, 0L, 0L), stdres = c(16.00425, 51.62291,
17.7373, 21.28994)), class = "data.frame", row.names = c(NA,
-4L)), eukaryote.list = structure(list(motif = c("ABG", "GBC",
"AGG", "CGG", "BTT"), obs = c(82015L, 51601L, 41922L, 25545L,
15795L), pred = c(48922.33, 64768.42, 30136.56, 14757.24, 12433.58
), prop = c(0.08773749, 0.11615591, 0.05404701, 0.02646569, 0.02229843
), stat = c(321891.7, 321891.7, 321891.7, 321891.7, 321891.7),
pval = c(0L, 0L, 0L, 0L, 0L), stdres = c(156.64562, -55.03402,
69.80141, 90.00215, 30.48747)), class = "data.frame", row.names = c(NA,
-5L)))
Created on 2022-05-23 by the reprex package (v2.0.1)
#allancameron: Elegant solution! Thanks. The key then is to row-bind so that the data can be facet-wrapped according to kingdom. I modified the code as below
plot in a single column-two rows
scale the y-axis according to the range of data
turn the y-axis labels 90 degrees for better readability.
library(ggplot2)
ggplot(dplyr::bind_rows(df.list.xyg, .id = "Kingdom"), aes(motif, stdres)) +
geom_col(width = 0.5, fill = "deepskyblue4") +
geom_hline(yintercept = 0, color = "gray75") +
facet_grid(Kingdom ~ ., scales = "free") +
theme_bw(base_size = 16)
Related
My goal is to plot a map with dwelling locations as points, where points are divided into two colours, based on a categorical variable, name category. Of those dwellings, a few dwellings need to have a different shape, e.g., a star. The column that describes this is called star in the example below. My dataframe looks like this:
x
y
category
star
123
456
1
0
143
556
0
0
124
556
1
1
233
256
1
0
ggplot(data = df, aes(x = x, y = y, color=category)) +
geom_point()
The code above gives me what I need, except for the 'stars'. How can distinguish this second column?
Have assumed you want the points with star with a value of 1 to be a star shape.
library(ggplot2)
ggplot(data = df1, aes(x = x, y = y, color=factor(category), shape = factor(star))) +
geom_point(size = 8) +
scale_shape_manual(breaks = c(0, 1),
values = c(1, 11))+
labs(color = "Category",
shape = "Star")
data
df1 <- structure(list(x = c(123L, 143L, 124L, 233L),
y = c(456L, 556L, 556L, 256L),
category = c(1L, 0L, 1L, 1L),
star = c(0L, 0L, 1L, 0L)),
class = "data.frame", row.names = c(NA, -4L))
Created on 2022-10-13 with reprex v2.0.2
I'm new to R and started with basic plots. I have used a simple excel table which has the following columns- Family, Family Name, Product.ID, Sales, Time, Value
The table is similar to the below snapshot
I used the code below but for some reason the when I try to group the chart by Family Name it is just showing a single color and even the legends is not appearing for all values in Family Name
library("ggplot2")
library("data.table")
library("readxl")
Data<-read.xlsx("C:/Users/vc/Desktop/Sales Data.xlsx")
setDT(Data)
Plot<-ggplot(data = Data,
aes(x=Time,y=Value,group='Family Name',shape='Family Name',color='Family Name')) +
geom_line() +
geom_point()
I have attached the output graph and it doesn't make any sense.
Do you want this?
For depiction of date on x axis you need to convert variable to as.Date() first
library(tidyverse)
df <- data.frame(
stringsAsFactors = FALSE,
Family = c("19A", "19A", "19A", "19A", "19B", "19B", "19B", "19B"),
Family.Name = c("A Box","A Box","A Box",
"A Box","B Box","B Box","B Box","B Box"),
Product.ID = c("AA980","AA980","AA980",
"AA980","AL345","AL345","AL345","AL345"),
Sales = c("actualsalesunit",
"actualsalesunit","actualsalesunit","actualsalesunit",
"actualsalesunit","actualsalesunit","actualsalesunit",
"actualsalesunit"),
Times = c("01-01-2021","01-02-2021",
"01-03-2021","01-04-2021","01-01-2021","01-02-2021",
"01-03-2021","01-04-2021"),
value = c(63L, 34L, 93L, 99L, 1L, 0L, 0L, 0L)
)
df %>%
mutate(Times = as.Date(Times, '%d-%m-%Y')) %>%
ggplot(aes(x= Times, y = value, color = Family.Name, label = value)) +
geom_line() +
geom_point() +
geom_text(size = 3.2, position =position_dodge(0.9), vjust = -0.5)
Created on 2021-05-27 by the reprex package (v2.0.0)
I am on R studio, and I am working on a graph that allows comparison between an input vector and what the database have.
The data looks like this:
Type P1 P2 P3
H1 2000 60 4000
H2 1500 40 3000
H3 1000 20 2000
The input vector for comparison will look like this:
Type P1 P2 P3
C 1200 30 5000
and I want my final plot to look like this:
The most important thing is a visual comparison between the input vector and the different types, for each P component. The scale of the y axis should adapt to each type of P, because there is big differences between them.
library(dplyr)
library(tidyr)
library(ggplot2)
d %>% gather(var1, val, -Type) %>%
mutate(input = as.numeric(d2[cbind(rep(1, max(row_number())),
match(var1, names(d2)))]),
slope = factor(sign(val - input), -1:1)) %>%
gather(var2, val, -Type, -var1, -slope) %>%
ggplot(aes(x = var2, y = val, group = 1)) +
geom_point(aes(fill = var2), shape = 21) +
geom_line(aes(colour = slope)) +
scale_colour_manual(values = c("red", "blue")) +
facet_grid(Type ~ var1)
DATA
d = structure(list(Type = c("H1", "H2", "H3"),
P1 = c(2000L, 1500L, 1000L),
P2 = c(60L, 40L, 20L),
P3 = c(4000L, 3000L, 2000L)),
class = "data.frame",
row.names = c(NA, -3L))
d2 = structure(list(Type = "C", P1 = 1200L, P2 = 30L, P3 = 5000L),
class = "data.frame",
row.names = c(NA, -1L))
I am looking to plot, in a barplot, the differences in value between two groups (Elevated Temp and Control).
I'd like to be able to plot these in the same way as my original graph with Months along the x axis.
Here is the following script I have used to get to the current barplot 1 that I have plotted. This shows y axis= plant growth and x axis=Months.
Script: Current Barplot
Tempmean<- data %>% group_by (Treatment, Month) %>% summarize (TTmean = mean(Amean, na.rm=TRUE), TTsd=sd(Amean,na.rm=TRUE))
p<-ggplot(data=Tempmean, aes(x=factor(Month), y=TTmean, fill=Treatment)) +
geom_bar(stat="identity", position="dodge", colour="black" , size = 0.25, width=0.5) + geom_errorbar(aes(ymin=TTmean-TTsd, ymax=TTmean+TTsd), width=.1,
position=position_dodge(.5)) + scale_fill_manual(values=c("darkgray","darkolivegreen")) + scale_x_discrete(breaks=6:8,labels=c("June","July","August")) + scale_y_continuous(limits=c(0,20), breaks=seq(0,20,2))
p
This is the data I am working with 2. I would be looking to take the TTmean of the eCO2 from the TTmean of the aCO2.
Data:
structure(list(Treatment = c("aCO2", "aCO2", "aCO2", "eCO2","eCO2", "eCO2"), Month = c(6L, 7L, 8L, 6L, 7L, 8L), TTmean = c(10.1922587348143,10.1061784054575, 8.27148533916994, 12.0261355594138,10.8954781586458, 10.9468200269188), TTsd =c(7.04936647397141,4.18653008350561, 1.50026716071241, 3.25471492346035, 0.742036555955107, 2.00464198948226)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Treatment", drop = TRUE, indices = list(0:2, 3:5), group_sizes = c(3L, 3L), biggest_group_size = 3L, labels = structure(list(Treatment = c("aCO2", "eCO2")), row.names = c(NA, -2L),class = "data.frame", vars = "Treatment", drop = TRUE))
This should do the trick.
I dropped TTsd because it doesn't seem like you need it. The trick is to spread() the data so you can easily compute the difference in the values. I computed as aCO2 minus eCO2; but you can change that in mutate()
library(tidyverse)
Tempmean %>%
select(-TTsd) %>%
# either group_by Month, or just ungroup entirely
group_by(Month) %>%
spread(Treatment, TTmean) %>%
mutate(T_diff = aCO2 - eCO2) %>%
ggplot(aes(factor(Month), T_diff)) %+%
geom_bar(position = "dodge", stat = "identity", size = 0.25, width=0.5) %+%
scale_x_discrete(breaks=6:8,labels=c("June","July","August"))
I don't know if my question clear enough...
I have this table
Name Mark_Oral Mark_Written Total_M_Oral Total_M_Written
1 Hercule Poirot 50 49 858 781
2 Joe O'Neil 70 79 1056 1083
3 John McAuley 81 99 1219 1333
and I have to visualize the last two column in bar chart using R to compare student total mark
Data
table <- structure(list(Name = c("Hercule Poirot", "Joe O'Neil", "John McAuley"),
Mark_Oral = c(50L, 70L, 81L),
Mark_Written = c(49L, 79L, 99L),
Total_M_Oral = c(858L, 1056L, 1219L),
Total_M_Written = c(781L, 1083L, 1333L)),
.Names = c("Name", "Mark_Oral", "Mark_Written", "Total_M_Oral", "Total_M_Written"),
row.names = c("1", "2", "3"), class = "data.frame")
You can use + to combine other plots on the same ggplot object. For example:
ggplot(survey, aes(often_post,often_privacy)) +
geom_point() +
geom_smooth() +
geom_point(aes(frequent_read,often_privacy)) +
geom_smooth(aes(frequent_read,often_privacy))
With ggplot2 (as your tags suggest) the syntax is:
ggplot(data = table,aes(x= Total_M_Oral,y=Total_M_Written))+geom_bar(stat = "identity")
Where table is replaced by the name of your dataframe.
Edit
I was unsure that my first answer really answered your question (multiple uses of bars).
Create dummy data
df<-data.frame(x = rpois(n = 100,lambda = 800),y = rpois(n = 100,lambda = 800))
With previous plot:
If you want to count and have a color for Oral and one for written
df2<-data.frame(x = c(df$x,df$y),y = rep(c("written","oral"),each = nrow(df)))
ggplot(data = df2,aes(x= x,fill=y),alpha = I(0.5))+geom_bar(stat = "count")
Which gives:
Comment: alpha parameter is not necessary, it just deals with the transparency so that you can see when there are overlapping bars.
With student names
df3<-data.frame(name = rep(table$Name,times = 2),
y = c(table$Total_M_Oral, table$Total_M_Written),
fill = rep(c("oral","written"),each = nrow(table)))
ggplot(data = df3, aes(x = name,y= y,fill = fill,alpha = 0.5))+geom_bar(stat= "identity")