Change direction of axis marks in a barplot - r

I need the following barplot with special axis marks. I tried for while but have difficulties getting it to work. Especially my axis-labels need to change their direction. I know that I have to use axTicks, axis and barplot commands. Anyone with an idea?
How it should look like:
Here is my data:
bpsamplevalues<-structure(c(21.3389252731795, 18.9930828477016, 19.4378755546201,
22.1009743407998, 23.8099463895258, 18.9706355343085, 19.4619810121121,
19.3433394825869, 26.8760997862876, 19.0948710373689), .Names = c("Div 1",
"Div 2", "Div 3", "Div 4", "Div 5", "Div 6", "Div 7", "Div 8",
"Div 9", "Div 10"))
I started with this code but I can not find a solution to get further:
barplot(bpsamplevalues, col="#87DEE1", axes=F, names.arg=F)

You may try this. It is the las argument which sets the orientation of axes labels. See ?par for more information.
barplot(bpsamplevalues, col = "#87DEE1", axes = FALSE, las = 2)
axis(side = 2, tick = FALSE, las = 1)
grid(nx = NA, ny = NULL, col = "white", lty = "solid")

Related

table1() Output Labeling all Data as "Missing"

I am trying to make a descriptive statistics table in R and my code functions properly (producing a table) but despite the fact that I have no missing values in my dataset, the table outputs all of my values as missing. I am still a novice in R, so I do not have a broad enough knowledge base to troubleshoot.
My code:
data <- read_excel("Data.xlsx")
data$stage <-
factor(data$stage, levels=c(1,2,3,4,5,6,7),
labels =c("Stage 0", "Stage 1", "Stage 2", "Stage 3", "Unsure", "Unsure (Early Stage)", "Unsure (Late Stage"))
data$primary_language <-factor(data$primary_language, levels=c(1,2), labels = c("Spanish", "English"))
data$status_zipcode <- factor(data$status_zipcode, levels = (1:3), labels = c("Minority", "Majority", "Diverse"))
data$status_censusblock <- factor(data$status_censusblock, levels = c(0:2), labels = c("Minority", "Majority", "Diverse"))
data$self_identity <- factor(data$self_identity, levels = c(0:1), labels = c("Hispanic/Latina","White/Caucasian"))
data$subjective_identity <- factor(data$subjective_identity, levels = c(0,1,2,4), labels = c("Hispanic/Latina", "White/Caucasian", "Multiracial", "Asian"))
label (data$stage)<- "Stage at Diagnosis"
label(data$age) <- "Age"
label(data$primary_language) <- "Primary language"
label(data$status_zipcode)<- "Demographic Status in Zipcode Area"
label(data$status_censusblock)<- "Demographic Status in Census Block Group"
label(data$self_identity) <- "Self-Identified Racial/Ethnic Group"
label(data$subjective_identity)<- "Racial/Ethnic Group as Identified by Others"
table1(~ stage +age + primary_language + status_zipcode + status_censusblock + self_identity + subjective_identity| primary_language, data=data)
Table output:
enter image description here
Data set:
enter image description here
When I run the data set the values are there. It actually worked for me when I re-did the spacing:
data$stage <- factor(data$stage,
levels = c(1,2,3,4,5,6,7),
labels = c("Stage 0", "Stage 1", "Stage 2", "Stage 3", "Unsure", "Unsure (Early Stage)", "Unsure (Late Stage"))
When I did it exactly as you typed it came up with NA's, too. Try the first and see if it works for you that way. Then check the spacing for the others. That may be all it is.
I do end up with one NA on the stage column because 0 is not defined in your levels.
Edit: Ran the rest so here are some other points.
You end up with an NA in stage because one of your values is 0 but it's not defined with a label
You end up with NA's in language because you have a 0 and a 1 but you define it as 1, 2. So you'd need to change to the values. You end up with NA's in other portions because of the :
Change your code to this and you should have the values you need except that initial 0 in "stage":
data$stage <- factor(data$stage,
levels=c(1,2,3,4,5,6,7),
labels =c("Stage 0", "Stage 1", "Stage 2", "Stage 3", "Unsure", "Unsure (Early Stage)", "Unsure (Late Stage"))
data$primary_language <-factor(data$primary_language,
levels=c(0,1),
labels = c("Spanish", "English"))
data$status_zipcode <- factor(data$status_zipcode,
levels = c(0,1,2),
labels = c("Minority", "Majority", "Diverse"))
data$status_censusblock <- factor(data$status_censusblock,
levels = c(0,1,2),
labels = c("Minority", "Majority", "Diverse"))
data$self_identity <- factor(data$self_identity,
levels = c(0,1),
labels = c("Hispanic/Latina","White/Caucasian"))
data$subjective_identity <- factor(data$subjective_identity,
levels = c(0,1,2,4),
labels = c("Hispanic/Latina", "White/Caucasian", "Multiracial", "Asian"))
enter image description here

Create empty data frame

I created a completely empty matrix. I would like to split a observation in 2 indices (like in Excel).
Indices <- matrix(NA, 8, 2)
rownames(Indices) <- rownames(Indices, do.NULL = FALSE, prefix = "Plot") # brauche ich das?
rownames(Indices) <- c("Plot 1", "Plot 2", "Plot 3", "Plot 8", "Plot 9", "Plot 10",
"Plot 12", "Plot 13")
colnames(Indices) <- c("Density", "Trees per ha")
I would like to split Densityone time in Density only Oaks and Density total. I have no idea how to call this, and is this even possible in R?

Invalid layers for facet_grid in ggplot2

I've been working with a variation on this code:
library(ggplot2)
Compare = matrix(c(
0, 0, "Soil 1", "tF",
0.379268025, 5.555806214, "Soil 1", "tF",
0.961561989, 13.05580621, "Soil 1", "tF",
1.55418685, 20.55580621, "Soil 1", "tF",
0, 0, "Soil 2", "tF",
0.104297312, 1.581249799, "Soil 2", "tF",
0.705818262, 6.081249799, "Soil 2", "tF",
1.447379092, 10.5812498, "Soil 2", "tF",
0, 20, "Soil 1", "tf",
0.379268025, 13.1603086, "Soil 1", "tf",
0.961561989, 12.72354396, "Soil 1", "tf",
1.55418685, 12.60549558, "Soil 1", "tf",
0, 20, "Soil 2", "tf",
0.104297312, 10.51383279, "Soil 2", "tf",
0.705818262, 6.433709727, "Soil 2", "tf",
1.447379092, 5.82398083, "Soil 2", "tf",
0, 0, "Soil 1", "zf",
0.379268025, 205.7706005, "Soil 1", "zf",
0.961561989, 483.5483783, "Soil 1", "zf",
1.55418685, 761.3261561, "Soil 1", "zf",
0, 0, "Soil 2", "zf",
0.104297312, 23.25367352, "Soil 2", "zf",
0.705818262, 89.43014411, "Soil 2", "zf",
1.447379092, 155.6066147, "Soil 2", "zf"), nrow = 24, ncol = 4, byrow = TRUE)
plot = ggplot(as.data.frame(Compare),
aes(as.double(Compare[,1]), as.double(Compare[,2]), color = Compare[,3])) +
geom_point() + facet_grid(Compare[,4] ~ .)
plot
My issue is with the facet_grid() aspect of the code. If I comment it out or delete it, it runs great, so I know I can isolate my issue to something that has to do with it. What I'm trying to accomplish is a set of three panels stacked vertically, with Compare[,1] on the x-axis, Compare[,2] on the y-axis, coloration based on Compare[,3] (producing a set of points for both soil types on each facet), and the three facets produced according to Compare[,4].
The error I get when I run with facet_grid() included in the code is:
Error in layout_base(data, rows, drop = drop) :
At least one layer must contain all variables used for facetting
I feel like my error may be related to the forcing of doubles and dataframes to deal with the initial form of a matrix, but am not sure what needs to be changed.
Your issues are more far-reaching than just the facet_grid component.
Matrices can only hold a single type of data. If you have multiple types, use a data.frame.
You as.double code is not correct for coercing from factor to numeric (as required because you had a character matrix coerced --> data.frame.
ggplot and aes should refer to the column names, not the direct subsets of the data object using [.
facet_grid is even more particular required names, not even functions thereof.
So, to rescue your data.
CP <- as.data.frame(Compare)
CP[[1]] <- as.numeric(as.character(CP[[1]]))
CP[[2]] <- as.numeric(as.character(CP[[2]]))
# your data.frame names are..
names(CP)
# [1] "V1" "V2" "V3" "V4"
ggplot(CP, aes(x = V1, y = V2, colour = V3)) +
geom_point() + facet_grid(V4 ~ . )

ggplot2 geom_text - 'dynamically' place label over barchart

I have what I know is going to be an impossibly easy question. I am showing an average number of days by month using a bar chart, using the following example:
dat <- structure(list(Days = c("217.00", "120.00", "180.00", "183.00",
"187.00", "192.00"), Amt = c("1,786.84", "1,996.53",
"1,943.23", "321.30", "2,957.03", "1,124.32"), Month = c(201309L,
201309L, 201309L, 201310L, 201309L, 201309L), Vendor = c("Comp A",
"Comp A", "Comp A", "Comp A", "Comp A",
"Comp A"), Type = c("Full", "Full",
"Self", "Self", "Self", "Self"
), ProjectName = c("Rpt 8",
"Rpt 8", "Rpt 8",
"Rpt 8", "Rpt 8",
"Rpt 8")), .Names = c("Days",
"Amt", "Month", "Vendor", "Type", "ProjectName"
), row.names = c("558", "561", "860", "1157", "1179", "1221"), class =
"data.frame")
ggplot(dat, aes(x=as.character(Month),y=as.numeric(Days),fill=Type))+
stat_summary(fun.y='mean', geom = 'bar')+
ggtitle('Rpt 8')+
xlab('Month')+
ylab('Average Days')+
geom_text(stat='bin',aes(y=100, label=paste('Avg:\n',..count..)))
Right now my labels are showing counts & showing up where ever i designate y.
I want to:
place labels at the top of the bars.
display the average, not the count.
I've pretty thoroughly - and unsuccessfully - tried most of the other solutions on SO & elsewhere.
Just got it:
means<-ddply(dat,.(Vendor,Type,Month), summarise, avg=mean(as.numeric(Days)))
ggplot(dat, aes(x=as.character(Month),y=as.numeric(Days),fill=Type))+
stat_summary(fun.y='mean', geom = 'bar')+
geom_text(data = means, stat='identity',
aes(y=avg+7, label=round(avg,0),group=Type))
i realize there is code nearly identical to this sitting elsewhere. my error came in placing the round's 0 outside the correct closing parenthesis -- thus moving all my labels to 0 on x axis... DUH!

Visualization of ranked likert-scale using advanced dot plot

I wish to produce a graph presenting responses to a number of agreement statements. The graph should allow for comparisons across the different groups of respondents and statement items.
I basically draw on a R code provided by Kastellec & Leoni (Figure 5; http://tables2graphs.com/doku.php?id=03_descriptive_statistics#figure_5).
In contrast to them, I would like to have the x-axis ranging from -5 to 5 and the table should be in the format of 2x2.
This code should produce sample data similar to the one I used:
mydata<-expand.grid(
col1=c('item1', 'item2', 'item3', 'item4'),
col2=c('0', '1', '3', '4'),
col3=c('T1', 'T2', 'C1', 'C2'))
mydata$col4=sapply(rnorm(64,0,1), function(x) {round(x,2)})
Note:
- col1: statement item („variable“ in the data set of K&L)
- col2: answer category ("period" in K&L)
- col3: group of respondent ("legislature" in K&L)
- col4: proportion ("proportion" in K&L)
And this is the code:
library(lattice)
library(car)
ltheme <- canonical.theme(color = FALSE)
ltheme$strip.background$col <- "lightgrey"
lattice.options(default.theme = ltheme)
mydata$col2<-factor(mydata$col2,
levels=c(0,1,3,4),
labels=c("strongly disagree", "disagree", "agree", "strongly agree"), ordered=TRUE)
mydata$col3<-factor(mydata$col3,
levels=c("T1", "C1", "T2", "C2"), ordered=TRUE)
levels(mydata$col3)<-c("treatment group 1", "control group 1", "treatment group 2", "control group 2")
mydata$col1<-factor(mydata$col1,
levels=c("item1", "item2", "item3", "item4"),
labels=c("item 4", "item 3", "item 2", "item 1"), ordered=TRUE)
prop.vec<-c(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)
plot<-dotplot(mydata$col1~mydata$col4|mydata$col3, xlab="levels of agreement",
data=mydata,
groups=mydata$col2,
layout=c(2,2),
scales=list(cex=0.65,
x=list(at=prop.vec),
alternating=3),
par.strip.text=list(lines=2.5,cex=0.65),
panel=function(...){
panel.abline(v=prop.vec, col="lightgrey")
panel.abline(h=1:11, col="lightgrey", lty=2)
panel.xyplot(...)},
as.table=TRUE,
par.settings=simpleTheme(pch=c(19,1,2,17), cex=0.7),
auto.key=list(space="bottom", column=4, cex=0.65)
)
trellis.device(file="figure.pdf", device="pdf", color=FALSE, width=6, height=8)
print(plot, newpage=FALSE)
dev.off()
Problem solved - I corrected the code so that it produces a nice plot presenting responses to statement agreements, as shown above. Feel free to further improve it, if you wish. Many many thanks for all your help!

Resources