Reputation: 118
Here is my code; works fine, except that alpha from ggplot trickles down to geom text label. Rather, I tried overriding it by specifying alpha again - doesn't work. Any workaround?
p <-
labdata_N %>%
ggplot(aes(x = reorder(type, measure), weight = measure, fill = type, alpha = Tobacco.Constituent)) +
scale_alpha_manual(values = c(0.4, 0.6, 0.8, 1)) +
geom_bar(position = "dodge", width = 0.75) +
geom_text(aes(label=Tobacco.Constituent, y = measure), position=position_dodge(width=0.75), vjust=-0.25, size = 3) +
scale_y_log10() +
labs(x = "SLT type", y = "Nitrosamine level") +
scale_fill_manual(values=mycols,
labels=c(type)) +
theme(legend.position = "bottom",
legend.title = element_blank(),
axis.text = element_text(size=12),
axis.title = element_text(size=14),
plot.title = element_text(size=14),
legend.text = element_text(size=9),
panel.background = element_rect()) +
annotation_logticks(side = "l") +
guides(fill = FALSE)
print(p)
Here is a slice of my data (let me know if u want it in a different format); Thanks, cheers!
1
NAB
HGT
60.56
2
NAB
HKH
375.01
3
NAB
HMH
3267.94
NAT
HGT
355.00
14
NAT
HKH
954.00
15
NAT
HMH
21062.00
NNK
HGT
229.62
26
NNK
HKH
249.87
27
NNK
HMH
10448.00
NNN
HGT
1303.00
38
NNN
HKH
3357.00
39
NNN
HMH
35603.00
Upvotes: 1
Views: 605
Reputation: 118
Using position = position_dodge2 in place of position = position_dodge solved the last problem. Here is the code that worked - hope someone in a few years can use it:
p <-
labdata_N %>%
Tobacco.Constituent)) +
ggplot(aes(x = reorder(type, measure), weight = measure)) +
geom_col(aes(alpha = Tobacco.Constituent, y = measure, fill = type), position = pos) +
geom_text(aes(label=Tobacco.Constituent, y = measure), size = 3, position = position_dodge2(width = 0.75)) +
scale_y_log10() +
labs(x = "SLT type", y = "Nitrosamine level") +
scale_fill_manual(values=mycols,
labels=c(type)) +
theme(legend.position = "bottom",
legend.title = element_blank(),
axis.text = element_text(size=10),
axis.title = element_text(size=10),
plot.title = element_text(size=10),
legend.text = element_text(size=10),
panel.background = element_rect()) +
annotation_logticks(side = "l") +
guides(fill = FALSE)
print(p)
the output is posted as pic;
Thanks @liborm ;
Upvotes: 0
Reputation: 2724
Just move the alpha
aesthetic mapping to the geom where it's really needed. Like
geom_bar(aes(alpha = Tobacco.Constituent))
The 'trickling down' is intentional, and it does not make much sense to let the aesthetic be inherited just to override it back in the next step..
Upvotes: 2