Included rounded medians in a box plot - r

So I know how to include the medians but I can’t seem to figure out how to round them.
This is what I have so far (if I mess up with how I include the code sorry this is my first time posting)
medians<-aggregate(FGPChange~convert,df,median)
df %>%
ggplot(aes(x=convert, y=FGPChange, fill=convert)) +
geom_boxplot(fill='dark red') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor
= element_blank(), axis.line = element_line(colour =
"black"))+
stat_summary(fun=median, geom="point",shape=18, size=3,
color="red",fill="red")+
geom_text(data=medians,aes(label=FGPChange,y=FGPChange+6.00))+

Related

How do I change the axis colour when using ggpredict() and plot()?

I'm trying to create a scatter graph of a linear model. I have successfully created the graph using ggPredict and plot but the axis is coming out light grey (barely visible) no matter what I put in to change it:
self_acc2 <- ggpredict(model1, "mean.self")
plot(self_acc2)
p.model7 <- plot(self_acc2)
self_acc <- p.model7 +
geom_point(data = dat_excluded, aes(x = mean.self, y = mean.acc),
alpha = 0.5, colour = "blue", shape = "circle") +
geom_line(size = 1) +
xlim(0, 9) +
ylim(0, 1) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor= element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black")) +
xlab("Self-Rated Accuracy") +
ylab("Mean accuracy") +
ggtitle("Relationship Between Self-Rated Accuracy and Actual Accuracy of Health Understanding")
self_acc
I used:
theme(axis.line = element_line(colour = "black"))
but this didn't affect the colour
I also tried:
plot(self_acc, colors="bw")
but this didn't change anything.
I also need to put axis tick marks and nothing is working for that either.
(I've only been using R for a few months, sorry if this is really basic! I also don't know how to properly lay this question out so I hope this is ok)
You could use axis.line.x.bottom and axis.line.y in your theme. I used the mtcars dataset to make it reproducible:
library(ggeffects)
library(ggplot2)
model1 <- lm(mpg~hp, data = mtcars)
self_acc2 <- ggpredict(model1, "hp")
p.model7 <- plot(self_acc2)
p.model7 +
theme(
panel.grid.major = element_blank(),
panel.grid.minor= element_blank(),
panel.background = element_blank(),
axis.line.x.bottom = element_line(colour = "black"),
axis.line.y = element_line(colour = 'black'))
Created on 2023-01-14 with reprex v2.0.2
If you want to add tick marks you can use axis.ticks:
p.model7 <- plot(self_acc2)
p.model7 +
theme(
panel.grid.major = element_blank(),
panel.grid.minor= element_blank(),
panel.background = element_blank(),
axis.ticks = element_line(colour = "black"),
axis.line.x.bottom = element_line(colour = "black"),
axis.line.y = element_line(colour = 'black'))
Created on 2023-01-14 with reprex v2.0.2

R: overlapping tooltips when hovering in ggplotly

I have this code. Basically, when mousing over the ggplotly plot in the value=50, I have two labels overlapping each other so basically I cannot see the West label, only the South.
How can I prevent that from happening? What am I doing wrong? I would like to see both labels separated when mousing over.
library(ggplot2)
library(ggplotly)
data <- data.frame(
name=c( "A" ),
value=c( 30,40,50,50),
location=c("North","East", "West","South")
)
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() +geom_point() +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)
Thank you,
You can use jitter and give appropriate width and height to see the points separately. Then it displays the appropriate values when you hover. Try this
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() + # geom_point(position=jitter, width=0.1) +
geom_jitter(alpha=0.6, width=0.02, height=0.1)+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)

ggplot2: remove geom_tile wired top and bottom block

I tried to plot a matrix using geom_tile. However, I noticed there are two strange blocks appear at the top and bottom of my plot. My initial guess was these are ticks element. I've tried to specify the theme parameters as far as I know but no luck.
Basically, I want to remove the two wired blocks that I marked in red arrow. The left plot is something that I desired except the white block. The right plot is I tuned the plot.background in theme to show you there are something I don't know occupies the area.
Image here
I also attached the minimal code that could reproduce the left plot:
test2 <- matrix(runif(100*100),nrow = 100)
testdf <- test2 %>% reshape2::melt()
testdf$Var2 <- factor(testdf$Var2,levels=(seq(max(testdf$Var2),1))) # you could ignore this line
testdf %>% ggplot() + geom_tile(aes(x=Var2,y=Var1,fill=log2(value+1))) +
scale_fill_gradientn(colors = c("#ffffff","#f9c979","#ec8121","#b80217","#2f0006")) +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank()) + coord_equal()
These blocks are the result of ggplot2's default expansion of the scale. To get rid of these block set the expansion to zero via scale_y_continuous:
library(ggplot2)
library(reshape2)
test2 <- matrix(runif(100*100),nrow = 100)
testdf <- reshape2::melt(test2)
testdf$Var2 <- factor(testdf$Var2,levels=(seq(max(testdf$Var2),1))) # you could ignore this line
ggplot(testdf) +
geom_tile(aes(x=Var2,y=Var1,fill=log2(value+1))) +
scale_y_continuous(expand = c(0,0)) +
scale_fill_gradientn(colors = c("#ffffff","#f9c979","#ec8121","#b80217","#2f0006")) +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank()) + coord_equal()

Problems altering axis label font sizes

I have effectively a very simple question.
I am using ggplot2 code to alter the font size of axis text and labels. However, wherever I position the command, none of the changes are visible on the axes. All other commands are working, so I am getting the impression that something is 'overriding' the theme(axis.text..., axis.title...) command.
ggplot(Cannock, aes(x=Capacity,color=CPType)) +
geom_histogram(fill="white",position="identity",binwidth=3,lwd=1) +
labs(title="Cannock Chase",x="Capacity", y = "Count") +
theme(axis.text=element_text(size=14), axis.title=element_text(size=16,face="bold")) +
facet_grid(CPType ~ .) +
geom_vline(data=mu1, aes(xintercept=grp.mean, color=CPType), linetype="dashed",size=1) +
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(legend.position="none") +
theme(strip.text.y = element_text(size=8, fac[![enter image description here][1]][1]e="bold"), strip.background = element_rect(colour="white", fill="white")) +
coord_cartesian(xlim = c(0,100)) +
theme(strip.background = element_blank(), strip.text = element_blank())
Any pointers for this would be greatly appreciated. Many thanks!
I think it might be that you've called theme_bw() after you change your axis text formatting. Any formatting that you want to change from defaults needs to be changed after calling theme_bw. Additionally, just to be a little cleaner and tighter, you can combine all of your theme arguments into one group so that it's easier to keep track of what you're changing. Does the code below solve the problem?
You also specify strip.text and strip.background twice, with different settings, which is probably not what you want to do.
ggplot(Cannock, aes(x=Capacity,color=CPType)) +
geom_histogram(fill="white",position="identity",binwidth=3,lwd=1) +
labs(title="Cannock Chase",x="Capacity", y = "Count") +
facet_grid(CPType ~ .) +
geom_vline(data=mu1, aes(xintercept=grp.mean, color=CPType), linetype="dashed",size=1) +
theme_bw() +
coord_cartesian(xlim = c(0,100)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none",
strip.text.y = element_text(size=8, face="bold"),
strip.text = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
strip.background = element_blank())

geom_errorbar and geom_facet

The following code does not display properly the error bars:
rf.imp<- read.csv("importances_byaggregations.csv",head=TRUE,sep=",") #Changes when handling the data
rf.imp$flux <- as.character(rf.imp$flux)
rf.imp$flux<-factor(rf.imp$flux,levels=unique(rf.imp$flux))
rf.imp$aggregation <- as.character(rf.imp$aggregation)
rf.imp$aggregation<-factor(rf.imp$aggregation,levels=unique(rf.imp$aggregation))
cbbPalette <- c("#F0E442", "#CC79A7","#E69F00","#56B4E9", "#009E73") # Mimicking Python colors
rf.imp$rel.influence<-rf.imp$rel.influence*100
rf.imp$SD<-rf.imp$SD*100
limits <- aes(ymax = rf.imp$rel.influence + rf.imp$SD, ymin=rf.imp$rel.influence - rf.imp$SD)
ggplot(rf.imp, aes(variable,rel.influence,fill=variable)) +
geom_bar(stat="identity",position="dodge") + scale_fill_manual(values=cbbPalette)+
theme_bw(base_size = 32, base_family = "Helvetica")+
xlab("")+
ylab("Variable importance (%)")+
facet_grid(aggregation~flux)+
geom_errorbar(limits, width=0.5)+
scale_y_continuous(limits=c(-10,90))+
theme(legend.position="none",
strip.text.x = element_blank(),
strip.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=1))
I would like to obtaine the following figure, but with the geom_facets swaped.
However, I get something like this:
Am I doing something wrong?
Thanks!
Your minimal example is a little too long for me to dig into, but I strongly suspect that your problem comes from using absolute (rf.imp$...) references in your error bar limits. If you use
geom_errorbar(aes(ymax=rel.influence+SD,
ymin=rel.influence-SD), width=0.5)
I think that will fix the problem.

Resources