"Thermal Bar" plot in R? - r

I want to create a plot with a single vertical bar (colored continuously), with a mark on it showing the score for a particular person. Image:
I can generate the colored bar in ggplot, but only as a legend (not the actual plot). For example the legend resulting from the following is fine:
ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) +
geom_point() +
scale_color_gradientn(colors = rainbow(5))
Is there any way to do this? Any help would be really appreciated - I'm completely stuck on this.

ggplot(data.frame(y = 51), aes( y=y)) +
geom_tile(data = data.frame(y = 0:100),
aes(x= 0.5, y = y, fill = y)) +
geom_segment(aes(x=0, xend=1, yend=y)) +
geom_text(aes(label = y, x = 1), hjust = -0.3) +
coord_cartesian(clip = "off", xlim = c(0,1.2)) +
scale_fill_gradientn(colors = rainbow(5)) +
scale_x_continuous(labels = NULL) +
guides(fill = FALSE) +
theme_minimal() +
theme(line = element_blank()) +
labs(x="", y = "")

Related

How to adjust both the line and point shape size in a legend to be different sizes?

I'm trying to adjust the point and line size independently in my legend. I'm wanting to be able to discern between the dashed and solid line while also having my point shapes be distinctive enough overtop of the line in the legend. Right now, I can't seem to figure out how to make the line smaller - I've figured out how to adjust the point size though. Any help/thoughts are all appreciated; definitely still a newbie. Thanks!
I'll post an image and code below:
Image of figure with points enhanced, but still can't get line to size correctly in the legend
- Chase
ggplot(df, aes(x = Psych.Distress.Sum, y = Likelihood.Max, color = Feedback)) +
geom_smooth(method = "lm", se = T, aes(linetype = Feedback)) +
geom_jitter(aes(shape = Feedback)) +
labs(x = "Psychological Distress", y = "Endorsement of Max's Feedback Strategy") +
apatheme +
theme(axis.title = element_text(face="bold")) +
theme(legend.text = element_text(face="bold")) +
theme(legend.position = c(0.87, 0.13)) +
scale_color_grey(end = .5) +
theme(legend.key.height= unit(.5, 'cm'),
legend.key.width= unit(1, 'cm')) +
guides(colour = guide_legend(override.aes = list(size= 3, linetype=0)))
This is tricky. Here's a hacky way using two plots that are overlaid on top of each other using patchwork. To prove that the data are aligned, I made the 2nd plot's text be semi-transparent red, but we could make it totally transparent with color #FF000000. This method is a little brittle, since the plots will come out of alignment if they have different ranges or different formats. But if we adjust for that, they line up perfectly with no extra fuss.
Your question didn't include any sample data so I used the mtcars data set.
library(patchwork)
library(ggplot2)
# This layer has the `geom_smooth` and black axis text
(a <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
geom_smooth(method = "lm", se = T, aes(linetype = as.factor(am))) +
scale_color_grey(end = .5) +
guides(linetype = guide_legend(override.aes = list(size = 1.2))) +
labs(x = "Psychological Distress",
y = "Endorsement of Max's Feedback Strategy",
linetype = "Line legend", color = "Line legend") +
coord_cartesian(ylim = c(10, 35)) +
theme_classic() +
theme(axis.title = element_text(face="bold")) +
theme(legend.text = element_text(face="bold")) +
theme(legend.position = c(0.7, 0.8)))
# This layer has the `geom_jitter` and red semi-clear axis text
(b <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
geom_jitter(aes(shape = as.factor(am))) +
scale_color_grey(end = .5) +
guides(shape = guide_legend(override.aes = list(size = 3))) +
coord_cartesian(ylim = c(10, 35)) +
labs(x = "", y = "",
color = "Point legend", shape = "Point legend") +
theme_classic() +
theme(plot.background = element_blank(),
panel.background = element_blank(),
axis.text = element_text(color = "#FF000055")) +
theme(legend.position = c(0.7, 0.55)))
a + inset_element(b, 0, 0, 1, 1, align_to = "full")

How to manually change the fill color of barplot when stat='count' in ggplot2

Here is my data:
mydata=data.frame(compliance=c("Yes","Yes","No","Yes","Yes","No"),
doctor=c("Sam","Sam","Sam","Bob","Fred","Bob"))
I tried to creat the barplot as below:
ggplot(data=mydata, aes(x=doctor,fill=compliance)) +
geom_bar(stat="count",width=0.4) +
geom_text(stat='count',aes(label=..count..), vjust=-0.3) +
expand_limits(y = c(0, 3)) +
labs(y = NULL, x= NULL) +
theme(legend.title = element_blank()) +
scale_fill_manual(values = c("#00BFC4","#F8766D")) +
scale_fill_discrete(limits = c("Not found","Compliance"))
However, the bar color is still in grey, I'd like to know how to fix this problem, thanks!
Don't use two scale_.*. You can add labels in scale_fill_manual itself.
library(ggplot2)
ggplot(data=mydata, aes(x=doctor,fill=compliance)) +
geom_bar(stat="count",width=0.4) +
geom_text(stat='count',aes(label=..count..), vjust=-0.3) +
expand_limits(y = c(0, 3)) +
labs(y = NULL, x= NULL) +
theme(legend.title = element_blank()) +
scale_fill_manual(values = c("#00BFC4","#F8766D"), labels = c("Not found","Compliance"))

Scale the distance between ticks on x-axis in gglot r

I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)

writing the x-axis label on all facets without scaling

I want to write the x-axis on all facets but without the scale.
The output
I tried to use scales='free_x' with scale_x_discrete(drop=FALSE) but it didn't work .
plot2 <- ggplot(counter2, aes(x = as.character(week) , y = freq,group=Delivery.time)) +
geom_point(colour = "red") + geom_line(colour = "red") +
labs(x = "Month", y = "Number Of Customers") +
facet_grid(.~Delivery.time) +
theme(axis.text.x = element_text(angle = 45, hjust=1)) +
geom_text(aes(label = freq),size = 2.5, hjust = .5, vjust=-0.5) +
facet_wrap(~Delivery.time, ncol = 1, scales = x) +
scale_x_discrete(drop=FALSE)
Plot with scaled x-axis
I want all a-axis

ggplot2 Creating a side and bottom legend

I'm trying to great a plot with one legend on the right side and one on the bottom. Both outside the map. I have tried a few suggestions but either both legends or neither legend moves.
library(ggthemes)
library(ggmap)
library(ggplot2)
d <- data.frame(McMap)
County_Mayo_Map <- get_map("MAP", zoom=9)
p <- ggmap(County_Mayo_Map)
p <- p + geom_point(data=d, aes(lat, lon)) +
geom_point(stat = "identity") +
geom_point(data = d, aes(color = Name), size = 5) +
scale_shape_manual(values=df$x) +
geom_point(data = d, aes(shape = Town), size = 4) +
labs(caption = ("SPM 8-19-17")) +
labs(title = "Title", subtitle = "Subtitle") +
ylab("Latitude") +
xlab("Longitude") +
coord_fixed(ratio = 1/1) +
theme(legend.background = element_rect(
fill ="lemonchiffon",
colour = "black",
size =1)) +
theme_solarized()

Resources