How to save a ggplot heatmap output as a raster - r

I have a data set which I wish to produce a heat map for and export the heat map as a raster so that I can use in another GIS software. I have the following data set;
structure(list(Longitude = c(83.01902, 83.04428, 83.07211, 83.08611,
83.11934, 83.12175, 83.13665, 83.14032, 83.15158, 83.15324, 83.17485,
83.17823, 83.19479, 83.21212, 83.22364, 83.24, 83.24301, 83.24766,
83.24869, 83.25096, 83.26048, 83.26767, 83.27696, 83.28758, 83.28842,
83.29257, 83.29861, 83.30751, 83.31339, 83.31459, 83.31735, 83.32446,
83.35895, 83.3939, 83.39481, 83.41, 83.49965, 83.5265, 83.54585,
83.54642, 83.56225, 83.58743, 83.6327, 83.67486), Latitude = c(27.49143,
27.53033, 27.54134, 27.589, 27.60669, 27.53022, 27.46776, 27.56822,
27.50877, 27.63619, 27.67205, 27.54466, 27.51068, 27.69448, 27.41738,
27.49342, 27.53017, 27.63331, 27.44174, 27.56867, 27.60454, 27.47598,
27.57293, 27.55558, 27.63096, 27.6531, 27.55854, 27.67777, 27.52301,
27.55364, 27.4924, 27.59005, 27.60943, 27.62901, 27.67632, 27.51055,
27.51166, 27.53252, 27.57101, 27.51175, 27.60436, 27.51426, 27.53022,
27.53555), Total = c(11L, 5L, 8L, 16L, 13L, 5L, 10L, 25L, 4L,
5L, 1L, 7L, 11L, 2L, 2L, 10L, 4L, 3L, 4L, 6L, 1L, 12L, 4L, 3L,
7L, 1L, 1L, 6L, 3L, 2L, 3L, 4L, 12L, 19L, 11L, 9L, 16L, 17L,
9L, 9L, 18L, 7L, 7L, 5L)), row.names = c(NA, -44L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), groups = structure(list(Longitude = c(83.01902,
83.04428, 83.07211, 83.08611, 83.11934, 83.12175, 83.13665, 83.14032,
83.15158, 83.15324, 83.17485, 83.17823, 83.19479, 83.21212, 83.22364,
83.24, 83.24301, 83.24766, 83.24869, 83.25096, 83.26048, 83.26767,
83.27696, 83.28758, 83.28842, 83.29257, 83.29861, 83.30751, 83.31339,
83.31459, 83.31735, 83.32446, 83.35895, 83.3939, 83.39481, 83.41,
83.49965, 83.5265, 83.54585, 83.54642, 83.56225, 83.58743, 83.6327,
83.67486), .rows = list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L)), row.names = c(NA,
-44L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
I have produced a heat map using ggplot with the following code
Data <- ggplot(Raptor_Fixed_Table_Sum, aes(x= Longitude, y= Latitude)) +
geom_point(data=Raptor_Fixed_Table_Sum, mapping=aes(x=Longitude, y=Latitude, size=Total))+
xlim(82.8,83.8)+
ylim(27.35,27.8)
Data + geom_density2d()+
stat_density2d(data=Raptor_Fixed_Table_Sum, aes(fill = ..level..),alpha=0.5, geom = "polygon")+
scale_fill_gradient(low = "green", high = "red")+
scale_alpha(range = c(0, 0.3), guide = FALSE)+
geom_point(data=Raptor_Fixed_Table_Sum, mapping=aes(x=Longitude, y=Latitude, size=Total), colour ="blue")
I don't know how to go about extracting just the heat map. Ideally I would like it as a raster so that I can save it and use in QGIS to overlay a map of the area.
Any help would be greatly appreciated.

Related

y-axis breaks with ggplot2 for a manhattan plot

I have a manhattan plot of genetic information:
It was generated using the qqman package https://cran.r-project.org/web/packages/qqman/vignettes/qqman.html) in R which takes a dataframe of P-values, chromosome position and a gene position (for any biologists reading, this is a per gene manhattan hence the sparsity of signal). The data looks like this (with an example dataset below:
SNP P CHR BP
ABC 1.1e-300 16 875849
AAS 1.2e-150 4 2343
JTL 4.2e-07 3 436544
LKS 4.1e-06 2 23565
JKSA 0.000432 1 98043
LKF 0.0032 22 387235
A20 0.0054 10 3252
AKLF 0.0235 4 4543543
structure(list(Gene = c("ABC1", "HGT2", "SLC34A3_ENSG00000198569",
"OR9K2_ENSG00000170605", "NFKB2_ENSG00000077150", "EFR3A_ENSG00000132294",
"SLC7A9_ENSG00000021488", "SEMG1_ENSG00000124233", "EWSR1_ENSG00000182944",
"ATP5PD_ENSG00000167863", "MAST3_ENSG00000099308", "KRT31_ENSG00000094796",
"FOXI1_ENSG00000168269", "CHCHD7_ENSG00000170791", "MAPK6_ENSG00000069956",
"SPRYD3_ENSG00000167778", "HOXB13_ENSG00000159184", "SLC12A9_ENSG00000146828",
"EXOC2_ENSG00000112685", "KCNJ15_ENSG00000157551", "SLC22A18_ENSG00000110628",
"ARID4A_ENSG00000032219", "SKP2_ENSG00000145604", "ZNF831_ENSG00000124203",
"ZNF275_ENSG00000063587", "SLC16A2_ENSG00000147100", "ADRB1_ENSG00000043591",
"DSCAM_ENSG00000171587", "PPM1H_ENSG00000111110", "IFNA14_ENSG00000228083",
"STX2_ENSG00000111450", "VPS54_ENSG00000143952", "ANXA7_ENSG00000138279",
"MAP3K12_ENSG00000139625", "MED13L_ENSG00000123066", "CHRM2_ENSG00000181072",
"RBP7_ENSG00000162444", "DRD1_ENSG00000184845", "CCDC121_ENSG00000176714",
"HMG20B_ENSG00000064961", "POU5F1B_ENSG00000212993", "SESN1_ENSG00000080546",
"DNASE1_ENSG00000213918", "FBXO24_ENSG00000106336", "RAG2_ENSG00000175097",
"UTS2_ENSG00000049247", "KMT2B_ENSG00000272333", "RBM33_ENSG00000184863",
"SNRPB2_ENSG00000125870", "FOXO4_ENSG00000184481", "NBPF3_ENSG00000142794",
"PPL_ENSG00000118898", "LYPD6B_ENSG00000150556", "POLD3_ENSG00000077514",
"PIK3CB_ENSG00000051382", "BCL2L12_ENSG00000126453", "CDC45_ENSG00000093009",
"DUXA_ENSG00000258873", "MCM3_ENSG00000112118", "CAPN3_ENSG00000092529",
"FMO4_ENSG00000076258", "B3GALT2_ENSG00000162630", "MICB_ENSG00000204516",
"CCL22_ENSG00000102962", "JKAMP_ENSG00000050130", "GSDME_ENSG00000105928",
"IZUMO4_ENSG00000099840", "NCKAP5L_ENSG00000167566", "ZRANB1_ENSG00000019995",
"TAL1_ENSG00000162367", "SLTM_ENSG00000137776", "SPC25_ENSG00000152253",
"GAP43_ENSG00000172020", "FGD3_ENSG00000127084", "PTCD3_ENSG00000132300",
"PAH_ENSG00000171759", "MMP8_ENSG00000118113", "RSBN1L_ENSG00000187257",
"AC026740.3_ENSG00000286094", "FAM189A2_ENSG00000135063", "TMEM245_ENSG00000106771",
"DDX50_ENSG00000107625", "SP140_ENSG00000079263", "C21orf91_ENSG00000154642",
"MEIKIN_ENSG00000239642", "TNFRSF8_ENSG00000120949", "RNF24_ENSG00000101236",
"CDK5_ENSG00000164885", "HINT1_ENSG00000169567", "TYRO3_ENSG00000092445",
"KRT75_ENSG00000170454", "RBM44_ENSG00000177483", "MYH8_ENSG00000133020",
"UBXN11_ENSG00000158062", "APOL3_ENSG00000128284", "NRXN3_ENSG00000021645",
"PRSS16_ENSG00000112812", "BST1_ENSG00000109743", "FAM49A_ENSG00000197872",
"SLC3A2_ENSG00000168003", "OR1C1_ENSG00000221888", "MYMK_ENSG00000187616",
"RASSF1_ENSG00000068028", "ARID5A_ENSG00000196843", "UAP1L1_ENSG00000197355",
"DPH2_ENSG00000132768", "G6PC_ENSG00000131482", "SH2B1_ENSG00000178188",
"RELL1_ENSG00000181826", "ABCC5_ENSG00000114770", "ZNF333_ENSG00000160961",
"NIF3L1_ENSG00000196290", "COMMD2_ENSG00000114744", "ZCCHC14_ENSG00000140948",
"P3H1_ENSG00000117385", "KRT14_ENSG00000186847", "SPG7_ENSG00000197912",
"ERCC6L_ENSG00000186871", "UPF1_ENSG00000005007", "FCGR3A_ENSG00000203747",
"SLC39A13_ENSG00000165915", "ACYP2_ENSG00000170634", "AL162596.1_ENSG00000285946",
"MEF2D_ENSG00000116604", "ATPAF1_ENSG00000123472", "DNAL4_ENSG00000100246",
"ADRA2A_ENSG00000150594", "ALDH3B2_ENSG00000132746", "L3MBTL3_ENSG00000198945",
"NR2E1_ENSG00000112333", "OTUD1_ENSG00000165312", "MCMDC2_ENSG00000178460",
"TXNL1_ENSG00000091164", "CES5A_ENSG00000159398", "CCL16_ENSG00000275152",
"ZBTB12_ENSG00000204366", "OGDHL_ENSG00000197444", "ARHGEF7_ENSG00000102606",
"RBM20_ENSG00000203867", "SELENOK_ENSG00000113811", "HBB_ENSG00000244734",
"WDR3_ENSG00000065183", "MAPKBP1_ENSG00000137802", "LTB4R2_ENSG00000213906",
"SLC25A15_ENSG00000102743", "ZBTB26_ENSG00000171448", "FDX2_ENSG00000267673",
"HSD3B7_ENSG00000099377", "RBFOX3_ENSG00000167281"), Pvalue = c(1.4e-300,
2.4e-150, 2.6089114579797e-07, 2.0296620694138e-06, 0.000147497259292417,
0.000229023886289315, 0.000245084674285079, 0.000256308708221289,
0.000261527824152563, 0.000288694716678695, 0.000290173032394758,
0.000320594572326915, 0.000346135729902497, 0.000355400110852,
0.000365256352980237, 0.000409731023356175, 0.000434204786603609,
0.000439775242591978, 0.000489192731765176, 0.000496753250110893,
0.00049911036273298, 0.000570787086811797, 0.000817460863988795,
0.000909350865229142, 0.000939159281654778, 0.00101875263711804,
0.00104161722087825, 0.00104642519111031, 0.0011025121215934,
0.00110797190460954, 0.00115516532029414, 0.00119237737210043,
0.00122886113380205, 0.00123316670384388, 0.00126924175390097,
0.00133083135434398, 0.00135900612361495, 0.00139601886941515,
0.00140034988031684, 0.00144667154281775, 0.00152488013161856,
0.00163920217629621, 0.00165121328565765, 0.00174281606991877,
0.00177541992540164, 0.00190567015024483, 0.00197012178338563,
0.00201154365191081, 0.00217761616500045, 0.00218849598206619,
0.00219107805420338, 0.00219952638949095, 0.0022100400174857,
0.00224988976742913, 0.00227842036080439, 0.00231351589815465,
0.00233840710255306, 0.00239368490047076, 0.00240800589782486,
0.00243072813003242, 0.00244930354205075, 0.00250643393459327,
0.00251262640919065, 0.00251308387281417, 0.00263512458389692,
0.00278748971622167, 0.00285692531240396, 0.00294631292976411,
0.0029855292366705, 0.00300042887433971, 0.00303321747691876,
0.00303431537337207, 0.00303655747990805, 0.00305247991142066,
0.00305779719421262, 0.0030773769185013, 0.00309595279588104,
0.00320602521859303, 0.00332374190234568, 0.00335845666631385,
0.00343476781423846, 0.00352132856036713, 0.0035370791144882,
0.00361921945446442, 0.00362829729460107, 0.00362925899436917,
0.00371857751928739, 0.00379170913533391, 0.00381786051662956,
0.00384603142808415, 0.0040621114920355, 0.00409131954647834,
0.00421076475281379, 0.00426968726537658, 0.00434706101829539,
0.00440972006588558, 0.00441860470852284, 0.00442578968523244,
0.00442716922579578, 0.00452215526426547, 0.00455658711791962,
0.00456768818316559, 0.00459525378983388, 0.00470562811526665,
0.00479427416502232, 0.00480697291736709, 0.00487609777383424,
0.00487626066774249, 0.0048982035968409, 0.00495106368869058,
0.00495974901689888, 0.0051182254688722, 0.00511868853158659,
0.00517459699358158, 0.0051863728177568, 0.0052533748441207,
0.0053048513357663, 0.00535144603215779, 0.00536294574878726,
0.00551084451782391, 0.00554884846488313, 0.0057184975334863,
0.00579274777888456, 0.00589230566622367, 0.00598698264647979,
0.00611781183554826, 0.00620691435617104, 0.00623285869674561,
0.00627192651777919, 0.00631120768525961, 0.00638288332792991,
0.00640000445930411, 0.00640676243762089, 0.00651734394089964,
0.0065624463096069, 0.00663922011120555, 0.00664879787639161,
0.00670461778135323, 0.00687266504207529, 0.00695679654393111,
0.00703352727799, 0.0070826001238915, 0.00709135444023445, 0.007142701991454,
0.00715597471729579, 0.00717318609326256, 0.00717726401691021,
0.00723420182380741, 0.00734437099984853), CHR = c(16L, 4L, 4L,
1L, 14L, 16L, 5L, 6L, 20L, 9L, 9L, 7L, 22L, 3L, 14L, 3L, 8L,
8L, 21L, 16L, 4L, 16L, 12L, 14L, 4L, 1L, 12L, 15L, 5L, 4L, 21L,
22L, 1L, 1L, 14L, 6L, 15L, 9L, 20L, 20L, 17L, 7L, 15L, 6L, 20L,
7L, 8L, 9L, 1L, 13L, 11L, 12L, 4L, 7L, 20L, 12L, 7L, 5L, 12L,
21L, 5L, 8L, 14L, 9L, 10L, 17L, 21L, 19L, 4L, 21L, 18L, 21L,
7L, 12L, 21L, 2L, 15L, 7L, 14L, 15L, 4L, 12L, 5L, 14L, 21L, 8L,
21L, 15L, 18L, 12L, 11L, 20L, 2L, 22L, 14L, 17L, 3L, 4L, 14L,
15L, 9L, 7L, 20L, 15L, 18L, 15L, 19L, 13L, 15L, 6L, 7L, 8L, 3L,
4L, 21L, 7L, 18L, 4L, 13L, 16L, 14L, 22L, 2L, 2L, 6L, 16L, 15L,
8L, 7L, 19L, 13L, 6L, 21L, 8L, 18L, 22L, 19L, 21L, 16L, 2L, 4L,
5L, 15L, 6L, 3L, 21L, 15L, 4L, 11L), POS = c(40665L, 197088L,
107291L, 210681L, 43546L, 79324L, 84342L, 184478L, 153093L, 180926L,
186110L, 117933L, 40682L, 54752L, 42758L, 61354L, 60378L, 157811L,
154466L, 126398L, 31037L, 115113L, 151914L, 10177L, 149587L,
79681L, 199754L, 129963L, 127032L, 175940L, 213708L, 51165L,
2584L, 166487L, 56259L, 130923L, 89219L, 170034L, 178967L, 102826L,
16982L, 188528L, 185007L, 6373L, 23298L, 199514L, 10429L, 58720L,
124518L, 210323L, 52212L, 186662L, 166963L, 58802L, 97157L, 14448L,
205795L, 70401L, 41824L, 93825L, 107954L, 207638L, 58648L, 64942L,
184005L, 19239L, 326L, 167713L, 106774L, 9145L, 174348L, 116079L,
38916L, 561L, 140433L, 123765L, 92497L, 187902L, 32027L, 63696L,
141286L, 67825L, 131698L, 120443L, 72621L, 165143L, 188862L,
52376L, 16769L, 77430L, 38655L, 145317L, 188469L, 113143L, 198322L,
26732L, 165043L, 25287L, 72392L, 12505L, 134208L, 126649L, 86308L,
199525L, 204348L, 103538L, 78610L, 176290L, 175950L, 73590L,
148494L, 151769L, 135252L, 141200L, 73351L, 45244L, 136493L,
33343L, 11165L, 915L, 80714L, 164700L, 142935L, 137224L, 554L,
92823L, 143083L, 166581L, 121459L, 19037L, 325L, 59959L, 155468L,
20896L, 33721L, 4468L, 113639L, 17103L, 184481L, 164337L, 174760L,
96405L, 207423L, 46590L, 168811L, 205743L, 74180L, 178456L, 126892L
)), row.names = c(NA, -149L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x55a80de817a0>)
In reality there are around 20,000 lines for each gene in the human genome.
Using qqman, one uses:
manhttahn(gwas_data...)
To get the plot.
I would like the same plot but with the axis broken between 8-149 and then again from 149-300 so that the bottom part isn't all compressed. qqman is unable to do this.
I have tried modifying the script from this website: https://danielroelfs.com/blog/how-i-create-manhattan-plots-using-ggplot/
And my code looks like this:
table above: gwas_data
data_cum <- gwas_data %>%
group_by(CHR) %>%
summarise(max_bp = max(BP)) %>%
mutate(bp_add = lag(cumsum(max_bp), default = 0)) %>%
select(CHR, bp_add)
gwas_data <- gwas_data %>%
inner_join(data_cum, by = "CHR") %>%
mutate(bp_cum = bp + bp_add)
axis_set <- gwas_data %>%
group_by(CHR) %>%
summarize(center = mean(bp_cum))
ylim <- gwas_data %>%
filter(P == min(P)) %>%
mutate(ylim = abs(floor(log10(P))) + 2) %>%
pull(ylim)
sig <- 0.05/length(gwas_data$P) #this is a bonferroni correction
manhplot <- ggplot(gwas_data, aes(x = bp_cum, y = -log10(P),
color = as_factor(CHR), size = -log10(P))) +
geom_hline(yintercept = -log10(sig), color = "grey40", linetype = "dashed") +
geom_point(alpha = 0.75) +
scale_x_continuous(label = axis_set$chr, breaks = axis_set$center) +
scale_y_continuous(expand = c(0,0), limits = c(0, ylim)) +
scale_color_manual(values = rep(c("#276FBF", "#183059"), unique(length(axis_set$chr)))) +
scale_size_continuous(range = c(0.5,3)) +
labs(x = NULL,
y = "-log<sub>10</sub>(p)") +
theme_minimal() +
theme(
legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title.y = element_markdown(),
axis.text.x = element_text(angle = 60, size = 8, vjust = 0.5)
)
This gives me:
Which is wrong. However, if I try and then cut the axis using the ggbreak package with:
t <- manhplot +scale_y_cut(break=c(10,140))
t+ scale_y_cut(break=c(140,300))
Which gives me:
How would I sort the chromosome x-axis and the breaks out so it looks like the qqman plot but with the y-axis compressed?
Many thanks

How to convert list to graph object in R?

I have adjacency list as shown here- dput(data)
list(c(2L, 3L, 5L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 18L, 22L,
32L), c(1L, 3L, 4L, 8L, 14L, 18L, 20L, 31L), c(2L, 4L, 9L, 10L,
14L, 28L, 29L, 33L), c(1L, 2L, 3L, 8L, 13L), c(1L, 7L, 11L),
c(1L, 11L, 17L), c(5L, 6L, 17L), c(1L, 3L, 4L), c(1L, 3L,
31L, 34L), c(3L, 34L, 33L), c(1L, 5L, 6L), c(1L, 32L, 23L
), c(1L, 4L, 2L), c(1L, 2L, 4L, 34L), c(33L, 34L, 29L), c(33L,
34L, 33L), c(6L, 7L, 19L), c(1L, 2L, 10L), c(33L, 34L, 14L
), c(1L, 2L, 34L), c(33L, 34L, 26L), c(1L, 2L, 15L), c(33L,
34L, 6L), c(26L, 28L, 33L, 34L), c(26L, 28L, 32L), c(24L,
25L, 32L), c(30L, 34L, 20L), c(3L, 24L, 34L), c(3L, 32L,
34L), c(24L, 27L, 33L), c(9L, 33L, 34L), c(1L, 25L, 26L,
33L), c(3L, 9L, 15L, 16L, 19L, 21L, 24L, 30L, 31L, 32L, 34L
), c(9L, 10L, 14L, 15L, 16L, 19L, 21L, 23L, 24L, 28L, 29L,
30L, 31L, 32L, 33L))
The list is similar to adjacency list and I wanted to convert it into igraph object in R. I have tried using
graph_from_data_frame(data, directed = FALSE, vertices = NULL)
but it is not working. I have also tried
graph_from_adj_list(data,mode="all",duplicate=FALSE)
but this is giving wrong graph. For example 1-> 2,3,5,6 should have edges as 1->2,1->3,1->5,1-6; but it is giving random output as 1->3,2->5,3->6,2->6 ...likewise.
Any idea, how this could be done in R?

How to compute standard errors for predicted data

I am trying to generate standard errors for predicted values. I use the below code to generate the predicted values but it fails to also give the standard errors.
ord6 <- veg$ord1-2
laimod.group = lmer(log(lai+0.000019) ~ ord6*plant_growth_form +
(1|plot.code) +
(1|species.code),
data=veg,
REML=FALSE)
summary(laimod.group)
new.ord6 <- c(-1,0,1,2,3,4,5,6,7)
new.plant_growth_form <- c("fern", "grass", "herb","herbaceous climber",
"herbaceous shrub", "moss", "tree sapling",
"undet", "woody climber", "woody shrub")
newdat <- expand.grid(
ord6=new.ord6,plant_growth_form=new.plant_growth_form)
newdat$pred <- predict(laimod.group,newdat, se.fit=TRUE, re.form=NA)
newdat
comment 1: laimod.group = final model selected after comparison of five models using lmer (package lme4)
comment 2: predictSE.mer requires package AICcmodavg
I did try the below code as an alternative but continue to receive the the following error message: Error in fam.link.mer(mod) : object 'out.link' not found
newdat$pred <- predictSE.mer(laimod.group, newdat, se.fit = TRUE, type = "response",
level = 0, print.matrix = FALSE)
Please see a reproducible subset of my data:
structure(list(plot.code = structure(c(1L, 2L, 3L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 13L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 16L, 17L, 18L, 19L, 19L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 29L,
29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 32L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 35L, 36L, 36L, 36L, 37L, 38L, 39L, 39L, 39L,
40L, 40L, 33L, 33L), .Label = c("a100f1r", "a100m562r", "a10m562r",
"a1f56r", "a1m5r", "b100f177r", "b100m17r", "b100m5r", "c100f17r",
"c100f1r", "c100f5r", "d100m56r", "d100m5r", "d10f1r", "d10f5r",
"e100m17r(old)", "e100m1r", "e100m5r", "e10f177r", "e10f17r(old)",
"e10f5r(old)", "e1f17r", "e1f5r", "f100m177r", "f10f177r", "f10f17r",
"f1m177r", "f1m56r", "lf1f1r", "lf1f5r", "lf1m1r", "og100f5r",
"og10f1r", "og10m1r", "og10m5r", "op100f562r", "op100m177r",
"op10f1r", "op10f5r", "op10m562r"), class = "factor"), species.code = structure(c(69L,
59L, 67L, 69L, 20L, 44L, 28L, 32L, 31L, 7L, 13L, 63L, 69L, 52L,
69L, 14L, 54L, 57L, 42L, 9L, 62L, 10L, 22L, 69L, 35L, 49L, 38L,
11L, 41L, 39L, 16L, 40L, 69L, 32L, 33L, 41L, 22L, 69L, 43L, 4L,
68L, 48L, 6L, 34L, 53L, 3L, 15L, 30L, 13L, 31L, 66L, 64L, 38L,
46L, 61L, 29L, 61L, 27L, 8L, 41L, 55L, 58L, 23L, 25L, 18L, 45L,
26L, 13L, 65L, 12L, 51L, 50L, 60L, 47L, 17L, 5L, 19L, 61L, 1L,
37L, 13L, 36L, 13L, 2L, 11L, 24L, 44L, 13L, 49L, 56L, 21L), .Label = c("agetri",
"alb214", "annunk", "arimin", "baudip", "beg032", "blurip", "buc009",
"cal079", "calplu", "chrodo", "cishas", "clihir", "cos049", "cycari",
"cypunk", "cyr075", "cyrped1", "dae205", "dalpin", "diapla1",
"dio063", "diosum", "emison", "ery046", "eryborb", "fic119",
"ficmeg", "friacu", "graunk", "indunk", "jactom", "lauunk", "leeind",
"luvsar", "lyccer", "mac068", "melmal", "mergra", "miccra1",
"mikcor", "mitken", "nep127", "nepbis", "paldas", "palunk", "panunk",
"penlax", "poaunk", "pol019", "pop246", "ptecog", "ptesub1",
"rubcle", "ryphul", "scamac", "scl051", "sclsum", "selcup", "selfro",
"spa098", "sphste1", "stitrut", "tet055", "tetdie", "tetdie1",
"tetkor", "xanfla", "zinunk"), class = "factor"), plant_growth_form = structure(c(3L,
6L, 9L, 3L, 7L, 1L, 7L, 4L, 8L, 5L, 5L, 1L, 3L, 7L, 3L, 3L, 9L,
2L, 9L, 7L, 9L, 7L, 7L, 3L, 9L, 2L, 10L, 4L, 4L, 9L, 2L, 7L,
3L, 4L, 7L, 4L, 7L, 3L, 1L, 4L, 7L, 7L, 3L, 10L, 7L, 7L, 1L,
2L, 5L, 8L, 9L, 9L, 10L, 7L, 9L, 9L, 9L, 7L, 7L, 4L, 7L, 2L,
7L, 10L, 3L, 7L, 10L, 5L, 9L, 9L, 7L, 7L, 6L, 7L, 3L, 9L, 9L,
9L, 9L, 7L, 5L, 6L, 5L, 9L, 4L, 3L, 1L, 5L, 2L, 7L, 7L), .Label = c("fern",
"grass", "herb", "herbaceous climber", "herbaceous shrub", "moss",
"tree sapling", "undet", "woody climber", "woody shrub"), class = "factor"),
ord1 = c(9L, 5L, 7L, 9L, 4L, 4L, 5L, 5L, 5L, 2L, 9L, 5L,
4L, 6L, 8L, 6L, 3L, 3L, 5L, 3L, 4L, 5L, 3L, 5L, 3L, 9L, 6L,
4L, 4L, 6L, 2L, 5L, 5L, 9L, 3L, 4L, 3L, 5L, 3L, 4L, 1L, 8L,
1L, 5L, 7L, 6L, 9L, 1L, 9L, 1L, 4L, 4L, 2L, 5L, 2L, 3L, 5L,
1L, 3L, 3L, 3L, 2L, 6L, 5L, 2L, 6L, 5L, 2L, 5L, 3L, 6L, 5L,
6L, 3L, 3L, 4L, 7L, 4L, 6L, 1L, 2L, 2L, 4L, 3L, 3L, 3L, 3L,
4L, 4L, 3L, 3L), lai = c(4.525068022, 0.325399379, 0.229222148,
4.076350538, 0.006889889, 0.003279268, 0.037268428, 0.056032134,
0.013573973, 0.001304667, 0.696949844, 1.256477431, 0.122569437,
0.191398415, 1.606070777, 0.425381508, 0.03013251, 0.00181661,
0.017317993, 0.014455456, 0.102704752, 0.031065374, 0.000923601,
0.453384679, 0.017859983, 7.765697214, 0.127071322, 0.102178413,
0.049099766, 0.427983019, 4.22e-05, 0.229034333, 0.694745347,
0.068069112, 0.218354525, 0.05883256, 0.032252145, 0.304812298,
0.009320025, 0.036424481, 0, 0.326, 0.000201724, 0.286106787,
0.556249444, 0.274764132, 4.21, 0, 0.695663959, 0.000213763,
0.00476907, 0.000205017, 3.77e-05, 0.134661951, 0.005631489,
0.0971, 0.172154618, 5.91e-05, 0.000371101, 0.000145266,
0.013382779, 0.00025348, 0.11016712, 0.0616302, 0.018011524,
0.107619537, 0.189926726, 0.000857257, 0.041252452, 0, 0.00475341,
0.077329281, 0.633865958, 0.038182437, 0.015560589, 0.010375148,
1.515423445, 0.008559863, 0.003636564, 0.000424537, 0.002786085,
0.091458876, 0.014216177, 0.165042816, 0.009187705, 0.00115711,
0.000920496, 0.009072635, 0.001443384, 0.001595447, 0.023263507
)), .Names = c("plot.code", "species.code", "plant_growth_form",
"ord1", "lai"), class = "data.frame", row.names = c(NA, -91L))

Formatting x-axis with histogram in R

I want x- axis from 1 to 20 and y-axis from 1 to 6.
My data:
structure(list(HEI.ID = structure(c(12L, 9L, 14L, 19L, 20L, 1L,
7L, 5L, 11L, 3L, 10L, 18L, 2L, 8L, 6L, 15L, 13L, 17L, 4L, 16L
), .Label = c("BF", "CC", "DC", "ER", "IM", "MC", "ME ",
"MM", "MO", "OC", "OM", "OP", "SB", "SD", "SH", "SL", "SN", "TH",
"UN", "WS"), class = "factor"), X2007 = c(18L, 14L, 15L, 20L,
12L, 6L, 17L, 2L, 4L, 11L, 16L, 1L, 9L, 8L, 13L, 4L, 10L, 6L,
3L, 19L), X2008 = c(20L, 9L, 16L, 18L, 8L, 17L, 15L, 6L, 3L,
14L, 19L, 1L, 2L, 12L, 5L, 13L, 11L, 7L, 4L, 10L), X2009 = c(20L,
13L, 17L, 8L, 4L, 9L, 19L, 12L, 2L, 11L, 16L, 1L, 2L, 7L, 6L,
18L, 5L, 15L, 9L, 14L), X2010 = c(20L, 13L, 16L, 13L, 7L, 15L,
19L, 8L, 3L, 9L, 18L, 1L, 5L, 11L, 12L, 6L, 10L, 4L, 2L, 17L),
X2011 = c(20L, 2L, 16L, 14L, 6L, 10L, 17L, 8L, 3L, 15L, 19L,
1L, 4L, 18L, 13L, 11L, 8L, 12L, 4L, 7L), X2012 = c(20L, 12L,
19L, 13L, 8L, 14L, 15L, 10L, 11L, 9L, 17L, 2L, 7L, 18L, 5L,
16L, 3L, 4L, 6L, 1L)), .Names = c("HEI.ID", "X2007", "X2008",
"X2009", "X2010", "X2011", "X2012"), row.names = c(NA, -20L), class = "data.frame")
I use the following commands to draw histograms:
par(mfrow = c(3,4))
for(i in 1:20){
print(i)
hist(as.numeric(HEIrank11[i,-1]),nclass=12,,main='students/faculty',
xlab = STOF[i,1],cex.lab=1, cex.axis=1, cex.main=1, cex.sub=1)
}
But after using above commands, I get different number in x- axis and y-axis.
I don't understand what your plot would looks like. It's not clear from your question and data provided.
I've tried to plot it. Please comment if you think it's the way to go.
Considering dt is your data.frame
library(reshape)
dt <- melt(dt)
library(ggplot2)
ggplot(aes(x=HEI.ID, y = value, fill = variable), data = dt) +
geom_bar(stat = 'identity')
or
ggplot(aes(x=HEI.ID, y = value, fill = variable), data = dt1) +
geom_bar(stat = 'identity') +
facet_grid(variable ~.)
You could use xlim and ylim parameters in the hist function and control the axes using
axis:
par(mfrow = c(3,4))
for(i in 1:12){
print(i)
hist(as.numeric(HEIrank11[i,-1]),nclass=12,,main='students/faculty',
xlim=c(0, 21), ylim=c(0,6), xaxt='n', yaxt='n')
axis(1, at=c(0, 10, 20))
axis(2, at=0:6)
}
Do you really want your y-axis to go from 1 to 6? This will cut off parts of the bars.
Also, you iterate over all 20 rows for a grid with 12 plots. The code above gives the following plot:

Loop when the output for each iteration is a data.frame

I want to do the looping for the following data. The output for a single iteration is a data.frame. My code is:
Data <- structure(list(v = c(15L, 15L, 15L, 15L, 16L, 16L, 16L, 17L,
17L, 18L, 19L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 25L, 25L
), b = c(35L, 70L, 42L, 35L, 20L, 48L, 16L, 68L, 68L, 51L, 57L,
57L, 57L, 95L, 76L, 70L, 21L, 77L, 77L, 100L, 30L), r = c(7L,
14L, 14L, 14L, 5L, 15L, 6L, 16L, 20L, 17L, 9L, 12L, 18L, 19L,
19L, 10L, 5L, 14L, 21L, 12L, 6L), k = c(3L, 3L, 5L, 6L, 4L, 5L,
6L, 4L, 5L, 6L, 3L, 4L, 6L, 4L, 5L, 3L, 5L, 4L, 6L, 3L, 5L),
lambda = c(1L, 2L, 4L, 5L, 1L, 4L, 2L, 3L, 5L, 5L, 1L, 2L,
5L, 3L, 4L, 1L, 1L, 2L, 5L, 1L, 1L)), .Names = c("v", "b",
"r", "k", "lambda"), class = "data.frame", row.names = c(NA,
-21L))
library(AlgDesign)
BIB <- list()
for(i in 1:nrow(Data)){
BIB[[i]] <- data.frame(optBlock(~., withinData = factor(1:Data[i, "v"]), blocksize = rep(Data[i, "k"], Data[i, "b"]))$Blocks)
dimnames(BIB[[i]]) <- list(1:Data[i, "k"], paste("Block", 1:Data[i, "b"], sep = " "))
}
BIB
Is there an easy way to accomplish the same task?
BIB <- list()
for(i in 1:nrow(Data)){
BIB[[i]] <- data.frame(optBlock(~., withinData = factor(1:Data[i, "v"]), blocksize = rep(Data[i, "k"], Data[i, "b"]))$Blocks)
dimnames(BIB[[i]]) <- list(1:Data[i, "k"], paste("Block", 1:Data[i, "b"], sep = "_"))
}
print(BIB)

Resources