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
Related
This is my script for the plot,
data = data.frame(Kingdom = c("Bacteria", "Archaea"),
Total = c(273523, 2616))
sizeRange <- c(0,30)
library(ggplot2)
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
somebody, please tell me how can I get a connecting line between my y-axis label and the plot
My plot looks like this
I want something like this
A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black") +
geom_point(aes(size = Total),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.y=element_blank(),
axis.title.y = element_blank(),
axis.ticks.y=element_blank())
you can manually add segments, but then the alpha of your points will kind of show them.
Here is a try, altought it's not perfect if the x axis expend.
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
# Added the segments here before the points.
# I tried to alpha it but I can't figure out how to limit the
# segment to the point border.
geom_segment(x = rep(-100,2), xend = rep(0,2),
y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() + guides(alpha = "none") + # remove alpha from legend.
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
Hello I have Dataframe ploting scatter plot with color using other column values plot is looking fine but the colour values are start from 0-100 and most of the values are in between 50-100 so I wanted put more color to diffrentiat can any one suggest me how can I do with the R, I tried with viridis color it is also looking same 50-100 color is almost looking similar color
link for the data
https://drive.google.com/file/d/1EKhRwup3vUC3KVFOOh4XtKERIr8FQj3x/view?usp=sharing
code what I tried
df=read.table("test.txt",sep='\t', header=TRUE)
df = data.frame(df)
p=ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
You can play with a customized color palette and scale_colour_gradientn like this:
library(RColorBrewer)
library(ggplot2)
#Data
df <- read.delim(file='test.txt',stringsAsFactors = F)
#Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
sc <- scale_colour_gradientn(colours = myPalette(100))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
If you want more color try this:
#Palette 2
sc2 <- scale_colour_gradientn(colours = rainbow(7))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc2 +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
Update2: With breaks you can define limits for the color scale:
#Plot 3
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = rainbow(25),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
Update 3: If you want to have different colors you can mix different palettes like this:
#Plot
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = c(viridis::inferno(5),
viridis::plasma(5),
viridis::magma(5),
viridis::viridis(5),
rainbow(5)),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
Odd question, but I can't seem to find a neat way to manage it! I'm making a number of scientific figures in R--each has a whole bunch of plots (via ggplot2) stacked on top of each other (with plot_grid), matched up along the same x axis. So far so good, and I'm almost ready to export and polish up all of the figures in illustrator.
The last step is to impose limits on each plot such that space between each plot in the stacks is as narrow as possible. I've done it in a first pass way with:
test1<-ggplot(mtcars, aes(mpg, hp)) +
geom_line() +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
axis.line.y.right = element_line(colour = "black", size=1),
axis.line.y.left = element_line(colour = "black", size=1),
axis.ticks.y.right = element_blank(),
axis.text.y.right = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x.bottom = element_blank(),
axis.text.x =element_blank(),
axis.line = element_line(colour = "black", size =1),)
test2<-ggplot(mtcars, aes(mpg, drat)) +
geom_line() +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
axis.line.y.right = element_line(colour = "black", size=1),
axis.line.y.left = element_line(colour = "black", size=1),
axis.ticks.y.right = element_blank(),
axis.text.y.right = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x.bottom = element_blank(),
axis.text.x =element_blank(),
axis.line = element_line(colour = "black", size =1),)
test3<-ggplot(mtcars, aes(mpg, wt)) +
geom_line() +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
axis.line.y.right = element_line(colour = "black", size=1),
axis.line.y.left = element_line(colour = "black", size=1),
axis.ticks.y.right = element_blank(),
axis.text.y.right = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x.bottom = element_blank(),
axis.text.x =element_blank(),
axis.line = element_line(colour = "black", size =1),)
test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test
It looks like something I've done in the theme bit. Not sure--but help would be appreciated!
If you want the plots stacked with no space between them, you need to play with the plot.margin value in the theme call. You can assign negative values, and different units. So, something like this could do what you want:
library(ggplot2)
library(cowplot)
#>
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#> default ggplot2 theme anymore. To recover the previous
#> behavior, execute:
#> theme_set(theme_cowplot())
#> ********************************************************
my_theme <- theme(
plot.margin = unit(c(0, 0, -0.3, 0), "lines"),
axis.title.x = element_blank(),
# axis.line = element_line(colour = "black", size =1),
axis.line.y.right = element_line(colour = "black", size=1),
axis.line.y.left = element_line(colour = "black", size=1),
axis.line.x.bottom = element_blank(),
# axis.ticks.y.right = element_blank(),
# axis.ticks.x = element_blank(),
# axis.text.y.right = element_blank(),
axis.text.x =element_blank(),
)
test1<-ggplot(mtcars, aes(mpg, hp)) +
geom_line() +
my_theme
test2<-ggplot(mtcars, aes(mpg, drat)) +
geom_line() +
my_theme
test3<-ggplot(mtcars, aes(mpg, wt)) +
geom_line() +
my_theme
test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test
Created on 2019-11-24 by the reprex package (v0.3.0)
Just play with the plot.margin bottom or top margins to get what you need.
Also, I commented some lines of your theme call, as they were unnecesary, at least with the example provided. Feel free to uncomment if needed with your original data.
Don’t really understand the question and you need to post a reproducible example.
Are you asking how to limit the axis ranges? If so
+ scale_x_continuous(limits = c(0, 1))
Will adjust the x axis range. Similar function for y. Just adjust the 0,1. Add it for each of your plots in the grid.
Update:
gglist <- lapply(gglist, function(x) x + scale_x_continuous(limits = c(0, 1)) )
I have been asked to place a full border around my plot below:
Using panel.border = element_rect(colour = "black") results in losing in the plot becoming blank.
I can't use theme_bw() as it does not have the same functionality as the usual theme, the code I am currently using is below:
graph<-ggplot(d,aes(x=d$AOE, y=d$MEI)
)+
geom_point(shape=20, size=3)+
geom_rug()+
annotate("text", x = -1.1, y = 14000, label = "27/04/2011") +
annotate("text", x = -1.3, y = 10400, label = "03/04/1974") +
xlab("MEI")+
ylab("AOE")+
scale_y_log10()+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
graph
Any advice on how to get a full black border would be very much appreciated!
To use panel.border you also have to specify a blank fill using fill=NA.
Try this:
library(ggplot2)
ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=5)
)
You can use theme_bw() and theme() together. This should work:
# creating some data
set.seed(1)
d <- data.frame(MEI=rnorm(100), AOE=rlnorm(100, 10, 5))
# creating the plot
ggplot(d,aes(x=MEI, y=AOE)) +
geom_point(shape=20, size=3) +
geom_rug() +
scale_y_log10() +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=4))
this gives:
A solution without theme_bw() and inspired by #Andrie, but with the use of panel.background instead of panel.border:
ggplot(d,aes(x=MEI, y=AOE)) +
geom_point(shape=20, size=3) +
geom_rug() +
scale_y_log10() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=4, fill=NA))
this will give the exact same plot. The difference between panel.background and panel.border is that panel.background is drawn underneath the plot and panel.border is drawn on top of the plot.
If you use any of the panel. options you will get a border around each individual facet when facetting. If you want a border around the outside of the entire plot, title etc included, then use plot.background. E.g:
library(ggplot2)
ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
labs(title = "Hello plot!") +
facet_wrap(~cyl) +
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
plot.background = element_rect(colour = "black", fill=NA, size=5)
)
Created on 2021-06-22 by the reprex package (v2.0.0)
I am trying to make a plot with no information beyond the data. No axes; no grid; no title; just the plot.
But I keep getting extra margins and padding that I can't remove.
library(ggplot2)
library(grid)
theme_bare <- theme(
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.ticks.length = unit(0, "lines"), # Error
axis.ticks.margin = unit(c(0,0,0,0), "lines"),
legend.position = "none",
panel.background = element_rect(fill = "gray"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(c(0,0,0,0), "lines"),
plot.background = element_rect(fill = "blue"),
plot.margin = unit(c(0,0,0,0), "lines")
)
ggplot() +
geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
theme_bare
Produces this image:
What I want is this:
I can't figure out how to get rid of the blue and make the dark gray flush with the edges.
Could any one offer some advice?
Here is the way to plot only the panel region:
p <- ggplot() + geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
theme(line = element_blank(),
text = element_blank(),
title = element_blank())
gt <- ggplot_gtable(ggplot_build(p))
ge <- subset(gt$layout, name == "panel")
grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
From ggplot2_2.0.0 you can use theme_void:
ggplot() +
geom_area(data = economics, aes(x = date, y = unemploy), linetype = 0) +
theme_void()
try
last_plot() + theme(axis.ticks.length = unit(0.001, "mm")) + labs(x=NULL, y=NULL)
you may want to file a bug for the 0 tick length.
If you just want to remove the grid in theme_bw(), you can use:
+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())