I need to hard code the labels for the variables and I can't use the row names as they are way too long and very non descriptive and confusing. Unfortunately, I need to do it ad hoc and just hardcode the variable names that I need for my plots. At this point, I can't create another variable with more reasonable names as I have too many to do in a short time.
I'm trying to hard code the plots so that the following are the labels.
indicator1 = "motorcycle"
indicator2 = "tricycle"
indicator3 = "truck"
indicator4 = "car"
indicator5 = "minivan"
I tried the below code, but it didn't work.
library(dplyr)
library(data.table)
library(ggplot2)
## Data
df_1 <- data.frame(
indicator1 = c(1,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,1,0,0,1),
indicator2 = c(1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0),
indicator3 = c(1,0,1,0,1,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0),
indicator4 = c(0,1,0,0,1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0),
indicator5 = c(0,0,1,1,1,0,1,0,1,0,1,1,1,1,1,0,0,0,0,0))
print(df_1)
mean_1 = apply(df_1,2,mean)
mean_1
# need to sort the means
mean_sort = data.frame(sort(mean_1, decreasing = TRUE))
mean_sort
# Start the indicator subset code for the different sections.
# get row names
n = row.names(mean_sort)
n
# I only want the top 2 and bottom 2 variables.
df_indicator_tail = tail(mean_sort,2)
df_indicator_tail
df_indicator_head = head(mean_sort,2)
df_indicator_head
df_indicator_rbind = rbind(df_indicator_head,df_indicator_tail)
df_indicator_rbind
# get the mean column name back
names(df_indicator_rbind) = "mean"
df_indicator_rbind
# create a column which is the sequence of the sort so I can do plots in order of mean value not alphabetic
df_indicator_rbind$x = seq(nrow(df_indicator_rbind))
df_indicator_rbind
# Plot: Need to label the y-axis tick marks to reasonable variable names
ggplot(data=df_indicator_rbind) +
geom_col(aes(x=x, y=mean,fill=mean), position=position_dodge())+
theme(axis.title.y=element_blank())+
labs(title = "Index by Indicator",
y = "Index") +
coord_flip()
#labels = c(x,"truck","minivan","motorcycle","car") Didn't work.