How to make decimal appear on lollipop graph - r

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()
)

Related

How to change distance between rows on lollipop

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:

How to change font and lollipop size?

I want to increase the width of the sticks on my lollipop graph, and increase the size of the font of my y-labels. How would I go about doing that?
df_graph2 <- data.frame(
parameters = c("Cut back spending on food",
"Used up all or most saving",
'Increased credit card debt',
'Took money out of long-term savings',
'Borrowed money from family or friends',
'Pawned or sold possessions'),
values <- c(34.40,26.00,25.50,14.40,12.70,11.30))
df_graph2 %>%
ggplot() + aes(x=parameters, y=values) +
geom_segment( aes(x=parameters, xend=parameters, y=0, yend=values), color="gray82") +
geom_point( color="darkorange", size=4.2, alpha=0.9) +
geom_text(aes(label = paste(values,"%")), hjust = -.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'),
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()
)
Change the y axis label size using the size parameter inside the element_text call of theme(axis.text.y and change the line width of the lollipops with size = inside geom_segment
ggplot(df_graph2, aes(parameters, values)) +
geom_segment(aes(xend = parameters, y = 0, yend = values),
size = 2, color = "gray82") +
geom_point(color = "darkorange", size = 4.2, alpha = 0.9) +
geom_text(aes(label = paste(values,"%")), hjust = -.3, size=3.8) +
expand_limits(y = 100) +
coord_flip() +
theme_void() +
theme(plot.margin = margin(1, 1, 4, 1.1, "cm"),
axis.text.y = element_text(color = 'black', size = 12, hjust = 1))

How to move legend up closer to the x-axis label in ggplot2

I have am creating a function to create dumbbell graphs with the legend positioned on the bottom. However, it's too far away from the title of the x-axis. I wanted to move it up slightly so that it is 10 pixels below the x-axis.
Here's the code:
vertical_theme = theme_bw(base_family = "Georgia") +
theme(
panel.border = element_rect(color = "black", fill=NA),
axis.title.x = element_text(hjust=0.5, size = 10, margin=margin(t=10, b=10)),
axis.text.y = element_text(size=10, margin=margin(r=10), color="black", hjust=0),
axis.text.x = element_text(size=10, margin=margin(t=10), color="black"),
axis.title.y = element_blank(),
legend.title = element_blank(),
legend.position= "bottom",
legend.text = element_text(size = 10, margin = margin(r = 10)),
panel.grid.major.y = element_blank() ,
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(size=1),
panel.grid.minor.x = element_blank(),
plot.margin = margin(10, 30, 10, 10, "pt"))
dumbbell = function(df) {
ggplot(df, aes(pct_responses, Domain)) +
geom_line(aes(group=Domain)) +
geom_point(aes(shape=race), size=5, color="#3bbae0" ) +
vertical_theme +
scale_shape_manual(labels = c("Black Students", "White Students"),
values=c(15, 19)) +
scale_x_continuous(expand = c(0, 0),
limits=c(0,100),
breaks = seq(0, 100, by=20),
labels = function(x) paste0(x,"%")) +
labs(x = "% of Responses") +
scale_y_discrete(labels = wrap_format(40))
}
dumbbell(df)
Here's a screenshot (labels on y-axis removed because that data isn't public yet):
I tried to adjust the legend.position manually with legend.position = c(0.5, 0) (playing around with various different numbers) but then the legend overlaps with "% of Responses."
Use theme(legend.margin=margin(-10, 0, 0, 0)) to move the legend up. Adjust -10 as needed.

How to fix "Error: Aesthetics must be either length 1 or the same as the data (28): yintercept"?

I'd like to make a forest plot for my project. Since it is not a typical forest plot built-in any R package, I found the first figure of this page is helpful to my goal, a side table accompanied with the forest plot:
https://mcfromnz.wordpress.com/2012/11/06/forest-plots-in-r-ggplot-with-side-table/
The code which produces that particular figure is pasted below (the original link:https://github.com/nzcoops/blog_code/blob/master/forest_plot.Rmd)
The problem that I ran into is in the "data_table" step. An error pop up when I type the following in R:
data_table
Error: Aesthetics must be either length 1 or the same as the data (28): yintercept
I guess the issue came from geom_hlinein data_table.
After some online search and some try-and-error, I still cannot get rid of that error message and wonder if I can get some help here. Thanks in advance for your help.
--Code that particular produce the first figure:
library(ggplot2)
library(gridExtra)
dat <- data.frame(group = factor(c("A","B","C","D","E","F","G"), levels=c("F","E","D","C","B","A","G")),
cen = c(3.1,2.0,1.6,3.2,3.6,7.6,NA),
low = c(2,0.9,0.8,1.5,2,4.2,NA),
high = c(6,4,2,6,5,14.5,NA))
theme_set(theme_bw())
theme_update(
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0,0,0), "lines"))
p <- ggplot(dat,aes(cen,group)) +
geom_point(size=5, shape=18) +
geom_errorbarh(aes(xmax = high, xmin = low), height = 0.15) +
geom_vline(xintercept = 1, linetype = "longdash") +
scale_x_continuous(breaks = seq(0,14,1), labels = seq(0,14,1)) +
labs(x="Adjusted Odds Ratio", y="")
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
lab <- data.frame(V0 = factor(c("A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G"),, levels=c("G","F","E","D","C","B","A")),
V05 = rep(c(1,2,3,4),each=7),
V1 = c("Occuption","Active","","Inactive","","Inactive","","Recreation","Inactive","","Active","","Inactive","","Gender","Men","Women","Men","Women","Men","Women","OR",3.1,2.0,1.6,3.2,3.6,7.6))
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
The easiest fix would be separating geom_hline into 2 different calls
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5))) +
geom_hline(aes(yintercept=c(7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
data_table
Created on 2018-03-31 by the reprex package (v0.2.0).
You don't need to use aes() with geom_hline (only use aes() if you want a horizontal line for every row of your data.) You can just do:
geom_hline(yintercept = c(6.5, 7.5))
This is explained in the help, see ?geom_hline for more details.

Using R to create an image don't want the white border or the title to appear only the PNG image

I'm using the code below to try to create a PNG image that is only the image but there is still some text and a white border.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + theme(legend.position="none") + theme(axis.title = element_blank())
I've also tried the following.
theplot <- data %>% ggplot(mapping = aes(x,y)) +
geom_point(mapping = aes(color=z), alpha = alpha, size = 0.75) +
scale_color_gradient(low="green", high="blue") +
theme_void() + 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(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
I'm not very familiar with R so I'm not sure if ggplot is what I should be using to create just an image.
Is it ok like this ?
library(ggplot2)
library(ggthemes)
ggplot(mtcars, aes(mpg, wt)) + geom_point() +
theme(plot.margin=unit(c(0,0,0,0), unit="mm"))
+ theme_fivethirtyeight()
ggsave("myplot.png")
One can take a look at the code of theme_fivethirtyeight:
> theme_fivethirtyeight
function (base_size = 12, base_family = "sans")
{
(theme_foundation(base_size = base_size, base_family = base_family) +
theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"],
linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]),
axis.title = element_blank(), axis.text = element_text(),
axis.ticks = element_blank(), axis.line = element_blank(),
legend.background = element_rect(), legend.position = "bottom",
legend.direction = "horizontal", legend.box = "vertical",
panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]),
panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0,
size = rel(1.5), face = "bold"), plot.margin = unit(c(1,
1, 1, 1), "lines"), strip.background = element_rect()))
}
I must say I don't understand why that doesn't work with your custom theme. I don't see a major difference.

Resources