Reputation: 61
I have seen several topics on creating subscripts on barplots, but I cannot seem to get the function "expression" to work in my code. I have attached the code below ideally I want $K_a$, $N_{max}$, $C_{r50}$, $C_{s50}$, but I cannot figure out how to make that appear in the bargraph. There's a large likelihood I have coded the most in-efficient bar graph, so any suggestions would be appreciated!
library(ggplot2)
lowdose <- c(3.412681e-03, 1.700527e-03, 1.207575e-04, 8.716199e-03, 8.186021e-05, 1.286261e-03, 5.872725e-04, 7.124678e-02, 2.107681e-01, 3.543541e-01)
lowdoseparams <-c("V", "k", "ka", "\u03B1", "\u03B3", "\u03B7", "\u03C3", "Nmax", "Cs50", "Cr50")
#lowdoseparams <-expression(V, k, k_a, alpha, gamma, eta, sigma, Nmax, Cs50, Cr50)
lowdata <- data.frame(Parameters=lowdoseparams, Effects=lowdose)
p_low<-ggplot(data=lowdata, aes(x=Parameters, y=Effects)) +
geom_bar(stat="identity")+theme(axis.text=element_text(size=14),
axis.title=element_text(size=18))
p_low
Upvotes: 4
Views: 216
Reputation: 24129
Using this question as referenece: Subscript letters in ggplot axis label
Here is a solution using the expression function to create the x axis labels:
library(ggplot2)
lowdose <- c(3.412681e-03, 1.700527e-03, 1.207575e-04, 8.716199e-03, 8.186021e-05, 1.286261e-03, 5.872725e-04, 7.124678e-02, 2.107681e-01, 3.543541e-01)
lowdoseparams <-c("V", "k", "k[a]", "\u03B1", "\u03B3", "\u03B7", "\u03C3", "N[max]", "Cs50", "Cr50")
#define the lowdoseparams as factors in the same order as the labels
lowdoseparams<-factor(lowdoseparams, levels =lowdoseparams)
#create the labels
xlabels<-expression(V, k, k['a'], alpha, gamma, eta, sigma, N[max], Cs[50], Cr[50])
lowdata <- data.frame(Parameters=lowdoseparams, Effects=lowdose)
p_low<-ggplot(data=lowdata, aes(x=Parameters, y=Effects)) +
geom_bar(stat="identity")+
theme(axis.text=element_text(size=14),axis.title=element_text(size=18)) +
scale_x_discrete(labels= xlabels)
p_low
Looks like the xlabels vector of type "expression" cannot be added to the dataframe but must remain as an independent vector.
Upvotes: 3
Reputation: 28401
I guess you can use this unicode subscript lookup table. For example \U2090
is for a
library(ggplot2)
lowdose <- c(3.412681e-03, 1.700527e-03, 1.207575e-04, 8.716199e-03, 8.186021e-05, 1.286261e-03, 5.872725e-04, 7.124678e-02, 2.107681e-01, 3.543541e-01)
lowdoseparams <- c("V", "k", "k\U2090", "\u03B1", "\u03B3", "\u03B7", "\u03C3", "Nmax", "Cs50", "Cr50")
lowdata <- data.frame(Parameters = lowdoseparams, Effects = lowdose)
p_low <- ggplot(data = lowdata, aes(x = Parameters, y = Effects)) +
geom_col() +
theme(
axis.text = element_text(size = 14),
axis.title = element_text(size = 18)
)
p_low
Created on 2018-11-19 by the reprex package (v0.2.1.9000)
Upvotes: 1