How to change distance between rows on lollipop - r

I want to decrease the distance between the rows on my lollipop graph. Does anyone know how I would do this? Any assistance is appreciated it.
df <- data.frame(
parameters = c("Posting written messages online", "Text messages", "Phone calls",
"Online learning system", "Online video chat","Blackboard Collaborate"),
value = c(32,29, 19, 53, 51,26))
df %>%
ggplot(aes(x= parameters, y=value)) +
geom_segment( aes(x= parameters, xend= parameters, y=0, yend=value),
color="dodgerblue4", size=2) +
geom_point( color="darkorange2", size=4.2, alpha=0.9) +
geom_text(aes(label = paste0(value,"%")), hjust = -0.3, size=3.8,family="Arial") +
expand_limits(y = 100)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', , size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x.bottom = element_blank()
)

Try this for reordering:
library(ggplot2)
#Code
df %>%
ggplot(aes(x= reorder(parameters,value), y=value)) +
geom_segment( aes(x= reorder(parameters,value), xend= parameters, y=0, yend=value),
color="dodgerblue4", size=2) +
geom_point( color="darkorange2", size=4.2, alpha=0.9) +
geom_text(aes(label = paste0(value,"%")), hjust = -0.3, size=3.8,family="Arial") +
expand_limits(y = 100)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', , size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x.bottom = element_blank()
)
Output:

Related

How to remove labels from ggplot donut in R

I have visualized some data as a donut chart. Here is the code:
ggplot(region_average, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=state_code)) +
geom_rect() +
geom_label_repel( x=3.5, aes(y=labelPosition, label=label), size=3) +
scale_fill_brewer(palette=4) +
coord_polar(theta="y") +
xlim(c(2, 4)) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank()) +
labs(title = "Regional Distribution of Ad Reach",
subtitle = "Overall distribution for all ads studied")+
theme(
panel.grid = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text = element_text(color=txt_col, size=8, hjust = 0.4),
axis.ticks = element_blank(),
axis.title.y.left = element_blank(),
panel.background = element_rect(fill = bg, color = bg),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
plot.title = element_text(hjust=0,size=24, color=txt_col,lineheight=.8, face="bold", margin=margin(0,0,0,0)),
plot.subtitle = element_text(hjust=0,size=14, color=txt_col, margin=margin(5,0,20,0)),
plot.title.position = "plot",
plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0), size=8, color=txt_col, face="bold"),
plot.background = element_rect(color=bg, fill=bg),
plot.margin = margin(30,30,30,30),
legend.title = element_blank(),
legend.position = "none"
)
This is the picture it produces
How can I get rid of the 2.0 ... 4.0 on the left hand side and the 0.00/1 ...0.75 around the donut?
Thank you!
As #Allan Cameron says in the comments, axis.text = element_blank()resolves the issue.

Pasting ggplot objects horizontally

I'm trying to paste 16 different plots into one with a common x axis
I successfully made, plot and paste the graphs individually with grid.draw()
But i haven't been able to transform the x axis in a continuous way or at least remove the space between each graph
i attach some code to illustrate how the graphs were made.
plot1 = ggplot(map_snp %>% filter( chr == chr[1] ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col=palette[2],
fill=palette[2],
alpha = .2) +
xlab("Chromosome 1") +
ylab("SNP count") +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.line.x = element_line(colour = "black"))
par_plots <- list()
for (i in list_par) {
par_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[3],
fill= palette[3],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
impar_plots <- list()
for (i in list_impar) {
impar_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[2],
fill=palette[2],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
grid.newpage()
png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(plot1),
ggplotGrob(par_plots$`Chromosome 2`),
ggplotGrob(impar_plots$`Chromosome 3`),
ggplotGrob(par_plots$`Chromosome 4`),
ggplotGrob(impar_plots$`Chromosome 5`),
ggplotGrob(par_plots$`Chromosome 6`),
ggplotGrob(impar_plots$`Chromosome 7`),
ggplotGrob(par_plots$`Chromosome 8`),
ggplotGrob(impar_plots$`Chromosome 9`),
ggplotGrob(par_plots$`Chromosome 10`),
ggplotGrob(impar_plots$`Chromosome 11`),
ggplotGrob(par_plots$`Chromosome 12`),
ggplotGrob(impar_plots$`Chromosome 13`),
ggplotGrob(par_plots$`Chromosome 14`),
ggplotGrob(impar_plots$`Chromosome 15`),
ggplotGrob(par_plots$`Chromosome 16`),
size = "last"))
dev.off()
SNP MAP
I can get rid of most of the border, but not quiiiiite all. Here using an example from built in diamonds data set from ggplot2.
par_plots <- list()
cuts = unique(diamonds$cut)
for (c in as.numeric(cuts)) {
par_plots[[c]] = ggplot(diamonds %>% filter(as.numeric(cut) == c),
aes(x = price)) +
geom_histogram() +
scale_x_continuous(expand = expansion(0)) +
xlab(paste0(i)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
plot.margin = unit(c(0,0,0,0), "cm"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
library(grid)
grid.newpage()
# png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(par_plots[[1]]),
ggplotGrob(par_plots[[2]]),
ggplotGrob(par_plots[[3]]),
ggplotGrob(par_plots[[4]])))
dev.off()

How to make decimal appear on lollipop graph

I have the following code for a lollipop graph. For the bars with a value of 4, I would like the data label to read 4.00. How can I modify my code for this?
library(dplyr)
library(ggplot2)
#par(mar=c(1, 1, 1, 1))
#par(oma=c(0,0,2,0))
df_graph2 <- data.frame(
parameters = c("REED 530: Process & Acquisition of Language and Reading",
"EDU 603: Principles and Practices of Research ",
'PHEC 604: Human Movement and Physical Activity for the Elementary Classroom',
'REED 532: Reading Materials',
'EDUC 606: Developmental Theory & Experiential Growth ',
'SPED 551: Adapting Instruction in Diverse Classrooms',
'EDUC 661: Mathematics: Curriculum, Instruction & Assessment',
'EDUC 662: Science and Health: Curriculum, Instruction & Assessment',
'REED 531: Reading/Literacy Instruction'
),
values <- c(4.00,3.88,4.00,4.00,4.00,4.00, 3.75, 3.13, 4.00))
df_graph2 %>%
ggplot() + aes(x=parameters, y=values) +
geom_segment( aes(x=parameters, xend=parameters, y=0, yend=values), color="gray36", size=2)
+
geom_point( color="goldenrod2", size=4.2, alpha=0.9) +
geom_text(aes(label = paste(values)), hjust = -.3,size=3.8,family="Arial") +
expand_limits(y = 6)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)[![enter image description here][1]][1]
This could be achieved by formatting via e.g. scales::number(value, accuracy = .01:
Note: I wrapped your long labels using str_wrap because otherwise the numbers did not show up in the reprex.
library(dplyr)
library(ggplot2)
df_graph2 <- data.frame(
parameters = c("REED 530: Process & Acquisition of Language and Reading",
"EDU 603: Principles and Practices of Research ",
'PHEC 604: Human Movement and Physical Activity for the Elementary Classroom',
'REED 532: Reading Materials',
'EDUC 606: Developmental Theory & Experiential Growth ',
'SPED 551: Adapting Instruction in Diverse Classrooms',
'EDUC 661: Mathematics: Curriculum, Instruction & Assessment',
'EDUC 662: Science and Health: Curriculum, Instruction & Assessment',
'REED 531: Reading/Literacy Instruction'
),
values <- c(4.00,3.88,4.00,4.00,4.00,4.00, 3.75, 3.13, 4.00))
df_graph2 %>%
mutate(parameters = stringr::str_wrap(parameters, width = 40)) %>%
ggplot() + aes(x=parameters, y=values) +
geom_segment( aes(x=parameters, xend=parameters, y=0, yend=values), color="gray36", size=2) +
geom_point( color="goldenrod2", size=4.2, alpha=0.9) +
geom_text(aes(label = scales::number(values, accuracy = .01)), hjust = -.3,size=3.8,family="Arial") +
expand_limits(y = 6)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)

R color scatter plot with more color gradiant

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:

ggplot theme remove y-axis on two plots in gtable, keep on third plot

I am trying to remove the y-axis on two ggplots that are in a gtable with a third ggplot. I would like to show the y-axis for the leftmost graph in the gtable and remove the y-axis completely from the subsequent graphs; however, I would like the x-axis to remain on all plots.
My graph looks like this:
![nucleotide diversity][1]
[1]: image produced by code
library("ggplot2")
library("gridExtra")
library("gtable")
theme_set(theme_bw(base_size=16))
p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) +
ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(#axis.text.y = element_blank(),
#axis.ticks.y = element_blank(),
#axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
grid.arrange(p1,p2,p3, nrow=1)
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))
newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3])
gt1$widths[1:3] = as.list(newWidth)
gt2$widths[1:3] = as.list(newWidth)
gt3$widths[1:3] = as.list(newWidth)
# New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null"))
# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt3, 1, 3)
grid.newpage()
grid.draw(gt)
Your linked image is not showing, but here is my shot in the dark:
Change from this:
plot1 <- theme(#axis.text.y = element_blank(),...
to this:
plot1 <- theme(axis.text.y = element_text(),...
if you want to change the label do this:
plot1 <- ... + ylab("y-axis label")
You need to do two things:
One is set axis.title.x and axis.title.y as blank under options (opts).
The other one is set xlab("") and ylab("")
I have included a code snippet in case it helps:
ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name,
shape=shape))+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank())+
xlab("") +
ylab("")+
theme(text = element_text(size=15, colour = "black"),
axis.text.x = element_text(angle=0, vjust=1, colour = "black"),
axis.text.y = element_text(angle=0, vjust=1, colour = "black"),
axis.line = element_line(colour = 'black', size = 1),
axis.ticks = element_line(colour = 'black', size = 1),
axis.ticks.length = unit(0.3, "cm"),
axis.title.y=element_text(vjust=0.4),
legend.position = "none") +
geom_point(size=5)+
scale_color_manual("Status", values = mycolours) +
xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) +
ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))

Resources