How to plot a histogram of already summarized data in R? [duplicate] - r

I know you usually should use a barplot for categorical variables but in my case somebody split the continuous variable into groups and it would be nice to have a histogram anyway.
This is what I want to get (except as a histogram):
par(oma=c(2,0,0,0)) #so labels are not cut off
barplot(table(hhincome),ylab = "Frequency", main = "Netto houshold income",
border="black", col="grey",las=2)
(Note: A histogram has no space between bars and a x-axis)
Data:
hhincome <- structure(c(4L, 4L, 1L, 6L, 8L, 1L, 4L, 5L, 2L, 3L, 1L, 5L, 1L, 7L, 6L, 7L, 3L, 2L, 6L, 7L, 8L, 4L, 7L, 8L, 7L, 4L, 5L, 5L, 5L, 9L, 7L, 5L, 8L, 8L, 6L, 5L, 5L, 3L, 5L, 4L, 3L, 5L, 3L, 5L, 4L, 4L, 5L, 7L, 6L, 7L, 2L, 6L, 1L, 7L, 4L, 4L, 5L, 2L, 4L, 6L, 6L, 8L, 6L, 7L, 4L, 7L, 9L, 1L, 4L, 6L, 2L, 6L, 8L, 6L, 5L, 8L, 7L, 9L, 7L, 9L, 8L, 5L, 5L, 7L, 6L, 2L, 7L, 6L, 6L, 1L, 7L, 7L, 2L, 6L, 6L, 6L, 7L, 5L, 2L, 2L, 9L, 6L, 7L, 7L, 5L, 6L, 6L, 5L, 5L, 7L, 8L, 6L, 6L, 3L, 7L, 6L, 4L, 5L, 5L, 4L, 8L, 3L, 4L, 6L, 5L, 7L, 3L, 4L, 7L, 5L, 3L, 6L, 2L, 2L, 5L, 2L, 4L, 8L, 4L, 3L, 2L, 7L, 2L, 5L, 2L, 1L, 8L, 7L, 3L, 6L, 6L, 7L, 2L, 9L, 3L, 3L, 5L, 7L, 7L, 5L, 6L, 8L, 5L, 6L, 5L, 5L, 7L, 6L, 5L, 5L, 6L, 10L, 3L, 6L, 6L, 3L, 2L, 4L, 9L, 2L, 6L, 7L, 1L, 5L, 6L, 5L, 4L, 7L, 5L, 2L, 6L, 3L, 3L, 2L, 7L, 6L, 6L, 5L, 7L, 6L, 1L, 7L, 3L, 2L, 5L, 5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L, 8L, 11L, 3L, 7L, 6L, 7L, 8L, 8L, 2L, 2L, 3L, 2L, 5L, 6L, 5L, 7L, 4L, 7L, 2L, 7L, 2L, 2L, 4L, 7L, 6L, 9L, 8L, 5L, 1L, 6L, 3L, 10L, 1L, 7L, 4L, 7L, 5L, 6L, 8L, 4L, 8L, 4L, 5L, 8L, 6L, 7L, 7L, 8L, 7L, 7L, 6L, 7L, 5L, 7L, 9L, 5L, 7L, 4L, 2L, 7L, 3L, 6L, 3L, 8L, 5L, 2L, 6L, 7L, 7L), .Label = c("Less than 500 €", "500-900 €", "900-1300 €", "1300-1500 €", "1500-2000 €", "2000-2600 €", "2600-3500 €", "3500-4500 €", "4500-6000 €", "6000-8000 €", "8000€ or more"), class = "factor")

Thanks to Zheyuan Li I already have my answer. Instead of forcing a histogram, I can simply create a barplot that looks excatly like a histogram:
par(oma=c(2,0,0,0)) #so labels are not cut off
barplot(table(hhincome2), space = 0, # set space between bars to zero
ylab = "Frequency", main = "Netto houshold income",
border="black", col="grey",las=2)
axis(1, at = hhincome,labels = FALSE) # at x-axis at category borders
box()
Edit: I just found an alternative way:
h <- hist(as.numeric(hhincome2) #as.numeric converst factor levels to numeric values
, xlab = "", ylab = "Frequency", main = "Netto houshold income \n(with normal disttribution curve)",
border="black", col="grey",las=2,
xaxt='n') #this supresses the x-axis which would disply levels instead values
axis(1, at = hhincome2, labels = hhincome2, las=2) #just add factor level labels as labels
box()
This way it's possible to add a normal disttribution curve as well:
xfit<-seq(min(as.numeric(hhincome2)),max(as.numeric(hhincome2)),length=1100)
yfit<-dnorm(xfit,mean=mean(as.numeric(hhincome2)),sd=sd(as.numeric(hhincome2)))
yfit <- yfit*diff(h$mids[1:2])*length(as.numeric(hhincome2))
lines(xfit, yfit, col="black", lwd=2)

Related

How to filter out duplicates in 2-column data frame but in reverse orientation?

I have a data frame of pairs of genes. There are some pairs which are listed twice but in reverse orientation. How do I remove those pairs which are duplicates (but in reverse orientation)? Thanks!
> dput(all_pairs)
structure(list(gene1 = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
2L, 3L, 4L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 7L, 8L,
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("ASXL1", "BICRA",
"CCDC168", "HRAS", "MUC16", "NOTCH1", "OBSCN", "PLEC", "RREB1",
"TTN"), class = "factor"), gene2 = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("ASXL1", "BICRA",
"CCDC168", "HRAS", "MUC16", "NOTCH1", "OBSCN", "PLEC", "RREB1",
"TTN"), class = "factor")), out.attrs = list(dim = c(10L, 10L
), dimnames = list(Var1 = c("Var1=ASXL1", "Var1=BICRA", "Var1=CCDC168",
"Var1=HRAS", "Var1=MUC16", "Var1=NOTCH1", "Var1=OBSCN", "Var1=PLEC",
"Var1=RREB1", "Var1=TTN"), Var2 = c("Var2=ASXL1", "Var2=BICRA",
"Var2=CCDC168", "Var2=HRAS", "Var2=MUC16", "Var2=NOTCH1", "Var2=OBSCN",
"Var2=PLEC", "Var2=RREB1", "Var2=TTN"))), class = "data.frame", row.names = c(NA,
-90L))
This keeps only one copy of each pair, no matter what the orientation/order is:
all_pairs[!duplicated(t(apply(all_pairs, 1, sort))), ]

How to change responding variable in whole script at once?

I have data of a big experiment with more than 10 different agricultural traits (e.g. GPC, WGC, TW, TKW...). For every trait I'm using the same principle of analysis (same R scripts). My question is, is there a simple way of how to replace responding variable in whole script at once? Except of using find/replace option.
This is a part of code that I'm using. GPC is a responding variable, and after I do all the analysis on GPC, I have to do the same analysis for WGC, then TW and so on.
mod <- lmer(GPC ~ env/rep + gen*env + (1|env:rep:row) + (1|env:rep:col), data = df)
GEmean <- cast(gen ~ env, data = df, value = "GPC", fun = mean)
Is there a way, for example, of labelling responding variable in the whole script as V1 and then just changing on the beginning of the script what V1 really means, which trait it is?
Any suggestion (except of using find/replace option :D) is very welcome!
Thank you!
You may define the variable as character, then define a RHS and use paste with as.formula,
V1 <- "Y1"
rhs <- "~ X1/X2 + X2*X1 + (1|X1:X2:X3) + (1|X1:X2:X4)"
mod <- lme4::lmer(as.formula(paste(V1, rhs)), data=df)
and cast as usual.
reshape::cast(X3 ~ X2, data=df, value=V1, fun=mean)
# X3 2 3 4 5 6 7 8
# 1 1 NaN NaN 1.25829503 NaN NaN 0.24229907 NaN
# 2 2 NaN NaN NaN NaN -1.66993472 NaN NaN
# 3 3 0.69914031 0.52025487 -0.13089285 -0.45742957 0.68799299 -0.65833219 -0.8931447
# 4 4 0.07498591 -0.06759845 -0.15950271 0.47577484 -0.31345072 0.98613984 NaN
# 5 5 0.34686080 0.84569994 0.22288982 -0.09291759 0.07317659 0.48756834 1.0484134
# 6 6 NaN -0.47471895 -0.01001069 0.03145422 -0.35163411 -0.43344249 NaN
# 7 7 NaN 0.17256757 0.68457304 0.11125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, 7L, 5L, 5L, 5L, 3L, 6L, 5L, 5L, 7L, 3L,
5L, 4L, 6L), X6 = c(4L, 6L, 3L, 5L, 5L, 4L, 3L, 7L, 4L, 7L, 3L,
7L, 6L, 5L, 4L, 4L, 5L, 5L, 5L, 6L, 5L, 6L, 4L, 5L, 7L, 5L, 3L,
3L, 4L, 4L, 4L, 6L, 3L, 7L, 4L, 5L, 5L, 3L, 1L, 4L, 4L, 2L, 7L,
6L, 5L, 6L, 3L, 6L, 5L, 3L, 2L, 7L, 4L, 5L, 4L, 7L, 2L, 2L, 5L,
4L, 6L, 4L, 7L, 2L, 7L, 6L, 4L, 4L, 6L, 6L, 4L, 8L, 5L, 5L, 4L,
5L, 5L, 3L, 7L, 4L, 5L, 6L, 4L, 5L, 2L, 3L, 5L, 3L, 8L, 2L, 4L,
4L, 4L, 3L, 7L, 5L, 8L, 3L, 4L, 5L), X7 = c(4L, 2L, 2L, 4L, 5L,
5L, 7L, 6L, 5L, 6L, 6L, 4L, 5L, 5L, 6L, 4L, 7L, 4L, 6L, 3L, 2L,
5L, 7L, 5L, 5L, 5L, 9L, 7L, 9L, 5L, 4L, 6L, 8L, 4L, 5L, 4L, 4L,
4L, 5L, 4L, 6L, 5L, 5L, 2L, 4L, 4L, 5L, 5L, 3L, 7L, 5L, 6L, 7L,
5L, 3L, 5L, 5L, 4L, 6L, 3L, 5L, 3L, 4L, 3L, 4L, 5L, 5L, 7L, 7L,
2L, 4L, 7L, 4L, 6L, 5L, 7L, 5L, 4L, 8L, 6L, 8L, 8L, 3L, 4L, 6L,
3L, 6L, 6L, 5L, 2L, 5L, 1L, 7L, 2L, 6L, 7L, 3L, 4L, 7L, 3L),
Y1 = c(1.51381660212862, 0.504478762772218, -2.65378574088482,
1.43068637272239, -1.47684324896984, 1.58472803945312, -0.0922193857071082,
-0.823622560562235, -1.64806412194411, 0.944309458265371,
-0.356936363717069, -0.162015258645447, 0.12477819226619,
-0.288471307345202, -1.18190619708113, -2.39292785982372,
0.851281094397896, 0.941804900216781, -0.285843452847721,
-0.637399290834473, 1.01562437034261, -0.762266388284812,
0.57482379687021, -1.35903991270023, 0.901967856080433, 0.29778523652812,
-0.00277763319220751, -0.231661006943, 1.17555880002615,
1.1776067492362, 1.2999024919628, 2.39038283158516, -0.650154555398476,
-0.953405711072244, 2.07296020843138, 0.186067377477045,
1.27493204613466, 0.233083651874369, -1.35590587652704, 1.08696059670825,
0.597820644193322, -1.19390157791241, -0.452467982110459,
-0.325648996612789, 0.0285958447740426, -0.550033308585066,
-0.541233659925362, 0.213664554548748, 0.0896115607405887,
-2.22657886242746, -0.835933260512375, -0.3410168395334,
0.50787381134198, 0.531258570313673, -0.40901169495662, -0.810128911311157,
-0.432058431678758, -0.10219858745039, -0.254360597321428,
-0.154028509912877, -0.154412368645572, -2.30203996365628,
1.46595811311111, -1.28869300561031, -1.21053214855949, -0.268363291058948,
0.683392481853822, -0.319669130901838, 0.645175024187205,
-0.411699887457752, -0.191809472206714, -0.862892281569729,
-0.496653266818923, -1.82777471788403, -0.316512824335762,
-1.92056252600781, 0.162658786066582, 2.80586044363066, -1.90191012546795,
-1.37325883300246, -0.293687705724852, -0.891121986533703,
0.236105127173931, 1.7496394242358, 1.30113476661601, -0.681215096624127,
1.19455442244611, 0.850906768469736, 0.555565335896252, -0.883430008481552,
0.824345772604795, 0.687913712610013, 1.33576821950678, -0.808845875084407,
-0.0994675295096023, -0.797674770999511, -1.7176681053504,
-0.312094291087325, 0.480884995744134, 0.3981995835995),
Y2 = c(-1.53285914020452, 1.29122978508191, 0.921606245351757,
-0.724085415151949, 0.573933753842347, -1.75990865905637,
-0.913661932893341, 0.843278144910845, -0.427945566890583,
-0.785931108082936, 0.18126807207226, -0.98233866170379,
-1.69942556383645, 0.161515532679225, -0.205045971136367,
-0.197708729131696, 0.761530789270692, -0.361650445168573,
-0.284077129912569, -1.4375454413273, -1.87641468387563,
-0.466142684630522, -1.32675782016916, 0.0766439433592152,
-0.345061291806643, 1.20492953816262, -0.498983588300403,
-0.39017069355203, -0.585536568506376, -0.268542109333508,
-0.304956745639016, 0.6672974421784, -1.33856731439226, 0.260863470997641,
-0.229908534712624, 1.87494902300616, -1.58721820502111,
-1.27757830338367, -0.164744698727624, 0.822502689857642,
0.487841959844427, -0.644277240904474, 0.0252163852645532,
-1.98028441396859, 0.303527219762318, 1.27316308828519, 0.0240727927001065,
-0.42804136638075, -1.20095767996442, -0.632935247840562,
1.3552165382172, -0.343889633450751, -0.62589000162234, 1.36744315735339,
-1.79969327521372, -0.31448607863348, 0.0738981401409877,
-0.699306260552063, -0.699259392290336, 0.312939740584129,
0.61517017083612, 0.159816208307784, -0.0454478235356775,
-0.84835290813545, 0.60453967324254, -0.933994665859771,
-0.122862888262648, 0.499170848210818, -1.04662060255234,
0.24416562707757, -0.239847908251422, 0.892507692301903,
1.52229141743761, -1.34797780685531, 0.720419062498517, 0.0347853939462854,
0.0636886378960745, 1.09832215170708, 0.526957518899046,
-0.0574283783582475, -0.293913833682497, -0.308886513025504,
-0.0843275557037345, -0.761732958784744, 1.20579213927605,
0.53737587291791, 0.669172456819161, 0.1720175113333, 0.207695456368839,
0.129107054181012, 1.22029130711617, -0.388441420234782,
-1.08556449077231, 0.612473701706874, 0.81564534791706, -1.32961150152966,
0.859407793312078, -0.412360608341253, -0.188597814992205,
0.605501867368945)), row.names = c(NA, -100L), class = "data.frame")
You can try lapply on the two functions. e.g.:
cols <- c('GPC', 'WGC', 'TW') # or use the column numbers
mod <- lapply(df[, cols], function(x) lmer(x ~ env/rep + gen*env + (1|env:rep:row) + (1|env:rep:col), data = df))
GEmean <- lapply(df[ , cols], function(x) cast(gen ~ env, data = df, value = x, fun = mean))
The output should be lists of analytical results.

Error in pts when when calling function ordiellipse in VEGAN package: subscript out of bounds

I have a nice DCA ordination of some of my data, but can't produce an ordiellipse for all different dataclusters. My data is found in the bottom of this post.
What happens now: I have four different groups (Block A-D) with three clusters per group (three year categories). So, I want to end up with different delineated groups. When I run the ordiellipes I get the following error message:
Error in pts[gr, , drop = FALSE] : subscript out of bounds
In addition: Warning message:
In complete.cases(pts) & !is.na(groups) :
longer object length is not a multiple of shorter object length
and a graph as follows:
DCA ordination of four blocks of vegetation
This piece of code reproduces the error, but due to the reduced dataset presented here, the graph looks a bit different:
install.packages("vegan")
library(vegan)
{plot(site_scr_kikker, type="n", main="Kikkervalleien", xlab="DCA1 Eigenvalue = 0.62",
ylab="DCA2 Eigenvalue = 0.39")
points(site_scr_kikker, display = "sites", cex = 0.8, pch=10)
ordiellipse(site_scr_kikker, Years_KIKKER, kind="se", conf=0.95, lwd=2,
draw = "polygon", col=1:4, border=1:4,
alpha=63)}
DCA Axes Dataset:
structure(list(DCA1 = c(-0.554410061801955, 2.68272013411215,
2.68697635940812, 2.82668169800565, 2.80053527027075, 2.23642581481516,
2.35425133786973, 2.52415368531054, 2.83239838572004, 2.84069370354046,
2.77338234239721, 2.81710120200121, 3.02325331285456, 2.53697043507954,
3.05037536310673, 3.32304086730676, 2.94495328416423, 3.15022598269494,
3.39489992455406, 3.28769160043834, -0.350924337608413, 0.275699505382009,
0.297344502163647, 0.240762119868438, 0.228861788615913, 0.314964666243383,
0.371085287846039, 0.455145784364889, 0.652221371003541, 0.499839296442089,
0.379360398080226, 0.549933370572594, 0.399966004306952, 0.500218697886041,
0.441564088620194, 0.374702692230443, 0.382333410536051, 0.43285459912782,
0.428611459750847, 0.349092514843647, -0.888853907037661, -1.28333663263808,
-1.40792331844972, -1.38537198615101, -1.38995889090796, -1.3655773745443,
-1.31803656153966, -1.34826448701426, -1.34653537792753, -1.49305269877646,
-1.50814236008689, -1.41827597394111, -1.39602666811321, -1.4148816003514,
-1.49783699791751, -1.47003691605731, -1.42755467648435, -1.30533485632748,
-1.36950094020217, -1.23477912087743, 3.23114892464093, 2.9350886798946,
3.14124836945073, 3.26161277365282, 3.09515391638416, 3.1529521123077,
3.06459587965894, 3.10368520711438, 3.22697584876561, 3.53654928835111,
2.98450087615265, 3.270797532973, 3.26776719866589, 3.49199289032157,
3.22923990263853, 3.25429242878212, 3.04740856725947, 3.0826704683258,
3.13214804334072, 3.02742007198209, -0.117264033632094, -0.16440126600505,
-0.0448538849517754, -0.0426633870391433, 0.0330104299718532,
-0.0752808949299411, 0.117242046915944, 0.0416044435035445, 0.124146770645119,
0.0523946356429974, -0.110261999817611, -0.228252641183511, -0.188814210123203,
-0.290927018876809, -0.248633979863795, -0.0903889717097015,
-0.123459222045697, -0.149699086185127, -0.150112841061331, 3.01689076683526,
2.01577708020474, 2.03044077034707, 2.07207139315213, 2.12441461917371,
2.03701011931199, 2.01252790874418, 1.83219506720427, 2.04345013029757,
2.15504917885961, 2.06913115663176, 1.98989149024749, 1.99123174245595,
1.96507730677135, 1.95295285738276, 2.04095710166195, 1.84679490913208,
1.83479477688629, 2.06370280057877, 2.09660967186289, 0.541840589690319,
0.103220405988339, 0.145850580989204, 0.171702980416538, 0.0991444115624873,
0.163980634495489, 0.0100630096884953, 0.00653099371627297, -0.049057450717042,
-0.0731989798652191, 0.0484957737765508, -0.0813429375561661,
0.226394829075491, 0.118747426326434, 0.0785696207929674, 0.372080921641888,
0.228084973201013, 0.436449500065551, 0.380195760092951, 0.421054280535058,
-0.3407891239866, -0.770535673192646, -0.78726979249955, -0.605034153126869,
-0.79603463000109, -0.611191548761836, -0.479087063427777, -0.431712806416684,
-0.442179135680639, -0.359040655364315, -0.387751952086651, -0.333064178275891,
-0.245245634230479, -0.294664916205089, -0.325293571885643, -0.371714350289459,
-0.384076243072539, -0.364275416660051, -0.492176029276133, -0.360665042070641,
1.97498200909125, 1.71918456504906, 1.65998788992634, 1.66434225634425,
1.56633028293729, 1.74620235786651, 1.62590128379407, 1.50258825353478,
1.48820880624004, 1.42926003809109, 1.45513337793396, 1.42592371006012,
1.4963606424124, 1.44021703608174, 1.44438380462437, 1.47109090679392,
1.82139520526838, 1.43656718298432, 1.44873704214624, 1.6139306940386,
0.329534864476447, 0.242211052748716, 0.235001084932526, 0.203151203202996,
0.0621389966258401, 0.0944651233344451, 0.335947463398379, 0.34920131294113,
0.356337550057783, 0.413800173211847, 0.475501084593146, 0.636972497835927,
0.378416570342712, 0.405927373162309, 0.483958766985421, 0.313492417628128,
0.18082570013463, 0.213448692873988, 0.175969392011173, 0.306174433718341,
-0.661344430804266, -0.36312534912334, -0.531638029637394, -0.323308841681458,
-0.1705480775506, -0.320797820641974, -0.0112455616689928, 0.0058094693123143,
-0.173103348877858, -0.187484910613069, -0.140328782633759, -0.262935718115112,
-0.213706195115846, -0.201623466852359, -0.176562229774177, -0.129977719792298,
-0.214157064357283, -0.312304712680445, -0.321801942265119, -0.447307072541585
), DCA2 = c(1.55135681949654, 0.390676820301294, 0.298911889220322,
0.263998071977169, 0.318540344211798, 0.261720092088233, 0.185092505227324,
0.266125079431566, 0.394828240097056, 0.302396200887096, 0.427178178571868,
0.362329582087479, 0.329300702637127, 0.106852609024896, 0.0916401140801768,
-0.0498768808296606, 0.0568755736541453, 0.0409183688588972,
-0.00982842960758612, -0.0532614523308772, 2.37879922539826,
0.870845236307184, 0.875448127097767, 0.641275864686684, 0.642137889278431,
0.61287181240447, 0.46096369228661, 0.353139355245069, 0.30571197713629,
0.127480232335107, 0.155591712070341, 0.201701485575426, 0.164465659451652,
0.053079369473755, 0.0208974057538049, 0.146542798250278, 0.133527092556681,
0.0558014251324042, 0.0947450033654067, 0.146527814538444, 1.5634624218799,
-0.045338607959831, 0.0921067787998133, 0.100136516321785, 0.176555931155601,
0.1779356816878, 0.169352553487154, 0.159084219879744, 0.1416643202517,
0.046751076432749, -0.0143690327219694, 0.0854961342502074, 0.0502099136978105,
0.0730195528098192, 0.0853374008019263, 0.115531044767214, 0.0847573955605063,
0.163097640034325, 0.134198472975748, 0.275479651900967, 0.0460034929226141,
0.560715956164233, 0.37831200537774, 0.258027386145382, 0.388149229049795,
0.321257843490554, 0.403942482899889, 0.195339552141307, 0.151011302110764,
0.0876417694236817, 0.0587161407304979, 0.0994546033680268, 0.251510488850064,
0.122130974589908, 0.111911790245653, 0.165535261771949, 0.060970314956561,
0.225723170237567, 0.313941078588394, 0.231918137883269, 0.993679773684799,
0.881292795126892, 0.949549576326203, 0.820143650778247, 0.967230951435818,
0.913507935790706, 0.987962294037885, 0.89747403569919, 1.0281502304616,
1.03056849037379, 0.985558206829436, 0.956118614451869, 0.990861510942461,
1.03853618229401, 0.76894643786781, 0.71956843396122, 0.895677149723554,
1.04202078104011, 0.994362394242357, 0.45816044069548, -0.256799924265915,
-0.219215409286906, -0.274974314031124, 0.00673120866587418,
-0.34588695905374, -0.330796391785146, -0.284953089585678, -0.358994836114401,
-0.0877152907820218, -0.0179836181616615, -0.0514356092538941,
-0.0631722426274615, -0.321764014760995, -0.292880797095688,
-0.124966216219314, 0.0448721698628494, -0.0122687592139075,
-0.0293240055712474, 0.241689548511685, 0.258771228150735, -0.243978231909183,
-0.273670301716394, -0.346381197575676, -0.540924824745573, -0.578466142473874,
-0.881698449269004, -0.988487876600371, -0.874559759965791, -0.898043753863041,
-0.65177624643986, -0.897172266653606, -0.428027378766614, -0.618350571130815,
-0.650486911424929, -0.522529645458612, -0.540007295687359, -0.56048323820591,
-0.318120499195913, -0.233107811772576, 1.11444300342379, -0.464311522843928,
-0.671043500267456, -0.293709784912165, -0.48957037940714, -0.303799057505386,
-0.5014139212286, -0.446968540045644, -0.584723850846212, -0.768962102318167,
-0.473903387692755, -0.476071214131476, -0.738718937014587, -0.802748557174088,
-0.878862063849493, -1.03232927446183, -0.901938937530595, -0.90685531694932,
-0.835172924486567, -0.444400981243365, 0.0711913939922195, -0.376272209371,
-0.278328148225639, -0.37229823300335, -0.158396017104884, -0.221206427389147,
-0.356652754022269, -0.130851791393296, -0.208569352651987, -0.12330848067377,
0.119039186900003, -0.145975049001435, -0.0110773787283525, 0.154455358806736,
0.186221284305681, 0.0518734671667143, 0.0410707622863646, 0.295096579413462,
0.277622386022512, -0.0377429837590535, -0.126848197591401, -0.0574585504960616,
-0.250845634712495, -0.0177800130809138, -0.107737216176091,
-0.0631643821637247, 0.1010605032824, -0.0442202733629364, -0.372070473916875,
-0.533311401539873, -0.724584176353283, -0.865166680824871, -0.87656068793911,
-0.813421991975295, -0.839998556813832, -0.655707249050569, -0.534597066763741,
-0.378820955906015, -0.0722774697143169, -0.109467994974947,
-0.331582307211823, -1.28959124402666, -1.37962362889618, -1.43451046953702,
-1.38447488090246, -1.69236882979906, -1.44344360082209, -1.3915281556235,
-1.58096147044615, -1.68132043125815, -2.20367091829309, -2.5599499288299,
-2.31297384112025, -2.38435599310711, -2.20768782296035, -1.65607037944418,
-1.64014952994504, -1.69013789782066, -1.7017681151936, -1.61898692370139
)), .Names = c("DCA1", "DCA2"), row.names = c("01A01", "01A02",
"01A03", "01A04", "01A05", "01A06", "01A07", "01A08", "01A09",
"01A10", "01A11", "01A12", "01A13", "01A14", "01A15", "01A16",
"01A17", "01A18", "01A19", "01A20", "08A01", "08A02", "08A03",
"08A04", "08A05", "08A06", "08A07", "08A08", "08A09", "08A10",
"08A11", "08A12", "08A13", "08A14", "08A15", "08A16", "08A17",
"08A18", "08A19", "08A20", "18A01", "18A02", "18A03", "18A04",
"18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11",
"18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18",
"18A19", "18A20", "01B01", "01B02", "01B03", "01B04", "01B05",
"01B06", "01B07", "01B08", "01B09", "01B10", "01B11", "01B12",
"01B13", "01B14", "01B15", "01B16", "01B17", "01B18", "01B19",
"01B20", "18B02", "18B03", "18B04", "18B05", "18B06", "18B07",
"18B08", "18B09", "18B10", "18B11", "18B12", "18B13", "18B14",
"18B15", "18B16", "18B17", "18B18", "18B19", "18B20", "01C01",
"01C02", "01C03", "01C04", "01C05", "01C06", "01C07", "01C08",
"01C09", "01C10", "01C11", "01C12", "01C13", "01C14", "01C15",
"01C16", "01C17", "01C18", "01C19", "01C20", "08C01", "08C02",
"08C03", "08C04", "08C05", "08C06", "08C07", "08C08", "08C09",
"08C10", "08C11", "08C12", "08C13", "08C14", "08C15", "08C16",
"08C17", "08C18", "08C19", "08C20", "18C01", "18C02", "18C03",
"18C04", "18C05", "18C06", "18C07", "18C08", "18C09", "18C10",
"18C11", "18C12", "18C13", "18C14", "18C15", "18C16", "18C17",
"18C18", "18C19", "18C20", "01D01", "01D02", "01D03", "01D04",
"01D05", "01D06", "01D07", "01D08", "01D09", "01D10", "01D11",
"01D12", "01D13", "01D14", "01D15", "01D16", "01D17", "01D18",
"01D19", "01D20", "08D01", "08D02", "08D03", "08D04", "08D05",
"08D06", "08D07", "08D08", "08D09", "08D10", "08D11", "08D12",
"08D13", "08D14", "08D15", "08D16", "08D17", "08D18", "08D19",
"08D20", "18D01", "18D02", "18D03", "18D04", "18D05", "18D06",
"18D07", "18D08", "18D09", "18D10", "18D11", "18D12", "18D13",
"18D14", "18D15", "18D16", "18D17", "18D18", "18D19", "18D20"
), class = "data.frame")
Years_Kikker Dataset
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("2001_A", "2001_B",
"2001_C", "2001_D", "2008_A", "2008_C", "2008_D", "2018_A", "2018_B",
"2018_C", "2018_D"), class = "factor")

How to rename integers to factor based on key

I need to make new column of factors based on value of column Quadrat. There are 9 quadrats, and new column called Sponge would be something like:
"Old Growth" if Quadrat = 1,4,9
"Absent" if Quadrat= 3,6,7
"New Growth" if Quadrat = 2,5,8
I am sorry if answer is easy, I did check: How to convert integer to factor in R?
and also I am trying to use recode_factor. Here is my code:
library(dplyr)
key <- list(`1,4,9` = "Old Growth", `3,6,7` = "Absent", `2,5,8` = "New Growth")
df <- mutate(df, Sponge = recode_factor(Quadrat, key))
I get error:
Error in mutate_impl(.data, dots) :
Evaluation error: Vector 1 must be length 108 or one, not 3.
Real data has much more entries than the dataset I include here, if that matters. Thank you for any help.
df <- structure(list(Quadrat = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L,
9L, 9L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L,
5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L,
7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L,
9L, 9L, 9L), Month = structure(c(4L, 4L, 4L, 3L, 3L, 3L, 7L,
7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L, 2L,
2L, 9L, 9L, 9L, 4L, 4L, 4L, 3L, 3L, 3L, 7L, 7L, 7L, 1L, 1L, 1L,
8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L, 2L, 2L, 9L, 9L, 9L, 4L,
4L, 4L, 3L, 3L, 3L, 7L, 7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L,
6L, 5L, 5L, 5L, 2L, 2L, 2L, 9L, 9L, 9L, 4L, 4L, 4L, 3L, 3L, 3L,
7L, 7L, 7L, 1L, 1L, 1L, 8L, 8L, 8L, 6L, 6L, 6L, 5L, 5L, 5L, 2L,
2L, 2L, 9L, 9L, 9L), .Label = c("Apr", "Aug", "Feb", "Jan", "Jul",
"Jun", "Mar", "May", "Sep"), class = "factor"), PopDens = c(65.6011820777785,
18.4913752602879, 12.151802276494, 68.0740840677172, 50.9832500135526,
36.8684287818614, 52.0825074084569, 26.8776902493555, 49.2173263626173,
25.5460870559327, 5.4171769618988, 34.4303709487431, 44.3439512783661,
2.25230997451581, 61.2502326716203, 25.9035727053415, 32.339118222706,
24.1017888628412, 12.340617884649, 53.3521768709179, 26.0048255382571,
52.8581868957262, 31.9503199581522, 18.1601244299673, 34.228305231547,
2.09199664392509, 22.6402857622597, 4.48008164577186, 48.2082461479586,
65.4937081446406, 5.43837511213496, 32.8203339113388, 4.44421968702227,
19.8568186087068, 24.2561273102183, 12.3652934685815, 39.0541164302267,
16.1970243314281, 12.9826903613284, 36.3537323835772, 48.7148000504822,
11.5067498446442, 68.7493303583469, 60.7505214684643, 49.3874175737146,
63.0705459746532, 23.721419940237, 53.4379795142449, 57.7867246468086,
38.4747762591578, 8.43540686019696, 20.5636212413665, 28.7687741059344,
53.2144687068649, 32.0859562589321, 10.5120962983929, 53.4312571119517,
13.6547974413261, 31.3038802060764, 14.5005466006696, 6.03453303268179,
62.6867637028918, 17.7734197168611, 11.0327071261127, 51.4377708046231,
26.8335341704078, 9.81126144807786, 43.993699422339, 20.5123583010864,
14.9305799969006, 23.8019575944636, 39.1543961388525, 30.4534046472982,
61.2751477411948, 48.0770866076928, 59.4514226955362, 42.9857548968866,
23.0139948409051, 1.76873184926808, 33.1222371393815, 10.8652087603696,
24.5235243474599, 62.4086231633555, 55.6522683221847, 68.8337469024118,
48.2195318546146, 6.75986870843917, 57.7931131315418, 18.2255988919642,
40.8185531077906, 38.066848333925, 31.8611310839187, 22.2724406518973,
51.7982920755167, 29.2363496678881, 35.541056742426, 66.5265460675582,
28.267403066624, 40.5209824540652, 31.8187582066748, 67.2972998009063,
53.6718824433628, 42.6495425191242, 31.6603209995665, 44.3039192620199,
21.6216275517363, 66.9763269643299, 36.3314134527463)), .Names = c("Quadrat",
"Month", "PopDens"), row.names = c(NA, -108L), class = "data.frame")
If we are using recode_factor, then create the list with individual components instead of pasteed one
key <- setNames(as.list(rep(c("Old Growth", "Absent", "New Growth"),
each = 3)), c(1, 4, 9, 3, 6, 7, 2, 5, 8))
df %>%
mutate(Sponge = recode_factor(Quadrat, !!! key)) %>%
head
# Quadrat Month PopDens Sponge
#1 1 Jan 65.60118 Old Growth
#2 1 Jan 18.49138 Old Growth
#3 1 Jan 12.15180 Old Growth
#4 2 Feb 68.07408 New Growth
#5 2 Feb 50.98325 New Growth
#6 2 Feb 36.86843 New Growth
Use mutate with the factor function
df %>% mutate(Quadrat2 =
factor(Quadrat, levels = 1:9,
labels =rep(c("Old Growth", "New Growth", "Absent"),3)
)
)

Create histogram (not barplot) from categorical variable

I know you usually should use a barplot for categorical variables but in my case somebody split the continuous variable into groups and it would be nice to have a histogram anyway.
This is what I want to get (except as a histogram):
par(oma=c(2,0,0,0)) #so labels are not cut off
barplot(table(hhincome),ylab = "Frequency", main = "Netto houshold income",
border="black", col="grey",las=2)
(Note: A histogram has no space between bars and a x-axis)
Data:
hhincome <- structure(c(4L, 4L, 1L, 6L, 8L, 1L, 4L, 5L, 2L, 3L, 1L, 5L, 1L, 7L, 6L, 7L, 3L, 2L, 6L, 7L, 8L, 4L, 7L, 8L, 7L, 4L, 5L, 5L, 5L, 9L, 7L, 5L, 8L, 8L, 6L, 5L, 5L, 3L, 5L, 4L, 3L, 5L, 3L, 5L, 4L, 4L, 5L, 7L, 6L, 7L, 2L, 6L, 1L, 7L, 4L, 4L, 5L, 2L, 4L, 6L, 6L, 8L, 6L, 7L, 4L, 7L, 9L, 1L, 4L, 6L, 2L, 6L, 8L, 6L, 5L, 8L, 7L, 9L, 7L, 9L, 8L, 5L, 5L, 7L, 6L, 2L, 7L, 6L, 6L, 1L, 7L, 7L, 2L, 6L, 6L, 6L, 7L, 5L, 2L, 2L, 9L, 6L, 7L, 7L, 5L, 6L, 6L, 5L, 5L, 7L, 8L, 6L, 6L, 3L, 7L, 6L, 4L, 5L, 5L, 4L, 8L, 3L, 4L, 6L, 5L, 7L, 3L, 4L, 7L, 5L, 3L, 6L, 2L, 2L, 5L, 2L, 4L, 8L, 4L, 3L, 2L, 7L, 2L, 5L, 2L, 1L, 8L, 7L, 3L, 6L, 6L, 7L, 2L, 9L, 3L, 3L, 5L, 7L, 7L, 5L, 6L, 8L, 5L, 6L, 5L, 5L, 7L, 6L, 5L, 5L, 6L, 10L, 3L, 6L, 6L, 3L, 2L, 4L, 9L, 2L, 6L, 7L, 1L, 5L, 6L, 5L, 4L, 7L, 5L, 2L, 6L, 3L, 3L, 2L, 7L, 6L, 6L, 5L, 7L, 6L, 1L, 7L, 3L, 2L, 5L, 5L, 3L, 3L, 3L, 4L, 1L, 7L, 5L, 3L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, 2L, 2L, 3L, 2L, 5L, 6L, 5L, 7L, 4L, 7L, 2L, 7L, 2L, 2L, 4L, 7L, 6L, 9L, 8L, 5L, 1L, 6L, 3L, 10L, 1L, 7L, 4L, 7L, 5L, 6L, 8L, 4L, 8L, 4L, 5L, 8L, 6L, 7L, 7L, 8L, 7L, 7L, 6L, 7L, 5L, 7L, 9L, 5L, 7L, 4L, 2L, 7L, 3L, 6L, 3L, 8L, 5L, 2L, 6L, 7L, 7L), .Label = c("Less than 500 €", "500-900 €", "900-1300 €", "1300-1500 €", "1500-2000 €", "2000-2600 €", "2600-3500 €", "3500-4500 €", "4500-6000 €", "6000-8000 €", "8000€ or more"), class = "factor")
Thanks to Zheyuan Li I already have my answer. Instead of forcing a histogram, I can simply create a barplot that looks excatly like a histogram:
par(oma=c(2,0,0,0)) #so labels are not cut off
barplot(table(hhincome2), space = 0, # set space between bars to zero
ylab = "Frequency", main = "Netto houshold income",
border="black", col="grey",las=2)
axis(1, at = hhincome,labels = FALSE) # at x-axis at category borders
box()
Edit: I just found an alternative way:
h <- hist(as.numeric(hhincome2) #as.numeric converst factor levels to numeric values
, xlab = "", ylab = "Frequency", main = "Netto houshold income \n(with normal disttribution curve)",
border="black", col="grey",las=2,
xaxt='n') #this supresses the x-axis which would disply levels instead values
axis(1, at = hhincome2, labels = hhincome2, las=2) #just add factor level labels as labels
box()
This way it's possible to add a normal disttribution curve as well:
xfit<-seq(min(as.numeric(hhincome2)),max(as.numeric(hhincome2)),length=1100)
yfit<-dnorm(xfit,mean=mean(as.numeric(hhincome2)),sd=sd(as.numeric(hhincome2)))
yfit <- yfit*diff(h$mids[1:2])*length(as.numeric(hhincome2))
lines(xfit, yfit, col="black", lwd=2)

Resources