Using x and y-axis differ in log scaling in ggplot2 - r

I successfully plot my data using ggplot() in R. However, when I choose to have the y- and x- axis in log10 scaling, the first tick on the y-axis (0.01) is further apart for the intersection than the first tick on the x-axis (0.01). I need the x-axis to have the same "scaling" as the y-axis.
Here is my code. Also the data (use sep="\t"). And an image of how the graph looks for me. Im sorry the data is on an external link, I couldnt figure out how to give it to you as reproducible data otherwise!
FILE1 <- read.delim("example.txt", sep="\t", header = TRUE)
EXAMPLE_PLOT <- ggplot(FILE1, aes_string(x = colnames(FILE1)[1], y = colnames(FILE1)[2])) +
geom_point(size=4) +
ggtitle("EXAMPLE_PLOT") +
theme(plot.title = element_text(family="Calibri", color="black",
face="bold", size = 32, hjust=0)) +
theme(plot.background= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
theme(panel.background = element_blank())+
theme(axis.line.x = element_line(color="black", size = 1),
axis.line.y = element_line(color="black", size = 1))+
theme(axis.ticks = element_line(color="black", size = 1))+
theme(axis.ticks.length = unit(0.3,"cm"))+
theme(axis.title = element_text(family = "Calibri",
color="black", size=17, face="bold"))+
theme(axis.text.x = element_text(family = "Calibri", color="black",
size=14, face="bold"),
axis.text.y = element_text(family = "Calibri", color="black",
size=14, face="bold"))+
scale_x_log10(breaks=c(.01, .1, 1, 10, 100))+
scale_y_log10(breaks=c(.01, .1, 1, 10, 100))+
geom_smooth(method=lm)
EXAMPLE_PLOT
THE DATA

You forgot to add limits in the scale_x_log10 and scale_y_log10:
FILE1 <- read.delim("example.txt", sep="\t", header = TRUE)
EXAMPLE_PLOT <- ggplot(FILE1, aes_string(x = colnames(FILE1)[1], y = colnames(FILE1)[2])) +
geom_point(size=4) +
ggtitle("EXAMPLE_PLOT") +
theme(plot.title = element_text(family="Calibri", color="black",
face="bold", size = 32, hjust=0)) +
theme(plot.background= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
theme(panel.background = element_blank())+
theme(axis.line.x = element_line(color="black", size = 1),
axis.line.y = element_line(color="black", size = 1))+
theme(axis.ticks = element_line(color="black", size = 1))+
theme(axis.ticks.length = unit(0.3,"cm"))+
theme(axis.title = element_text(family = "Calibri",
color="black", size=17, face="bold"))+
theme(axis.text.x = element_text(family = "Calibri", color="black",
size=14, face="bold"),
axis.text.y = element_text(family = "Calibri", color="black",
size=14, face="bold"))+
scale_x_log10(breaks=c(.01, .1, 1, 10, 100), limits = c(0.01,10))+
scale_y_log10(breaks=c(.01, .1, 1, 10, 100), limits = c(0.01,10))+
geom_smooth(method=lm)
EXAMPLE_PLOT

Related

r adding line (new coordinates) to ggplot2 scaterplot

How can I add a line with new x coordinates to my scatterplot? I get an error saying the line x-values must match those from my scatterplot.. I tried using geom_line() as you can see
library(ggplot2)
x <- c(1,2,3)
y <- c(4,5,6)
a <- seq(0.5,5, by = 0.5)
b <- seq(1,10)
ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
geom_point(shape = 1) +
geom_point(aes(2.5,2.5, colour = "My Portfolio"),
shape = 18,
size = 3) +
geom_line(aes(a, b)) +
ggtitle("Efficient Frontier") +
xlab("Volatility (Weekly)") +
ylab("Expected Returns (Weekly)") +
theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.position = c(0.93,0.06),
legend.title = element_blank(),
legend.text = element_text(size=8),
legend.background = element_rect(color = "black"),
legend.key=element_blank())
thanks!
You have to provide a new data.frame to geom_line() and set the aestetics accordingly.
library(ggplot2)
x <- c(1,2,3); y <- c(4,5,6)
a <- seq(0.5,5, by = 0.5); b <- seq(1,10)
ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
geom_point(shape = 1) +
geom_point(aes(2.5,2.5, colour = "My Portfolio"),
shape = 18,
size = 3) +
geom_line(data=data.frame(a,b), mapping=aes(x=a, y=b)) +
ggtitle("Efficient Frontier") +
xlab("Volatility (Weekly)") +
ylab("Expected Returns (Weekly)") +
theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.position = c(0.93,0.06),
legend.title = element_blank(),
legend.text = element_text(size=8),
legend.background = element_rect(color = "black"),
legend.key=element_blank())

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

ggplot2 density plot legend shows wrong group names and wrong colors

The following code plots two partly overlapping density distributions from two independent dataframes with different lenghts.
library(ggplot2)
#Define colors to be used in plot for each group
mycolRO <- rgb(0.8, 0.2, 0, max = 1, alpha = 0.5) #Color for Group "Road"
mycolRA <- rgb(0.2, 0.6, 0.4, max = 1, alpha = 0.5) #Color for Group "Rail"
#Create some data
dfRoad <- data.frame(DiffRO=2+rnorm(300))
dfRail <- data.frame(DiffRA=rnorm(500))
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
theme_classic() +
ggtitle("") +
xlab("Value") +
ylab("Density") +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15))+
labs(fill = "Group")+
theme(legend.title = element_text(color = "black", size = 15), legend.text = element_text(color = "black", size=12))+
theme(legend.position = c(0.2,0.8), legend.direction = "vertical")+
guides(alpha=FALSE)
The legend does show the correct base color, but not with the transparency (alpha) value defined above, which should be alpha=0.5.
Furthermore I would like to see the correct variable names ("DiffRO" and "DiffRA") as legend entries instead of the color codes.
Thanks for any help.
Here are two ways of doing what you want.
Common points to both are:
The colors are set manually with scale_fill_manual.
theme calls are simplified, there is no need to call theme repeatedly.
First, I will recreate the data, this time setting the RNG seed before calling rnorm.
set.seed(1234)
dfRoad <- data.frame(DiffRO = 2 + rnorm(300))
dfRail <- data.frame(DiffRA = rnorm(500))
Your way, corrected.
The legend labels must also be set manually in scale_fill_manual.
#Plot density distributions
ggplot() +
geom_density(aes(x=DiffRO, fill = mycolRO, alpha=0.5), data=dfRoad) +
geom_density(aes(x=DiffRA, fill = mycolRA, alpha=0.5), data=dfRail) +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(labels = c("Road", "Rail"),
values = c(mycolRO, mycolRA)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical")+
labs(fill = "Group") +
guides(alpha = FALSE)
Another way, simpler.
The data is combined and reformated from two different data sets in one data set only. To do this I use package reshape2.
dflong <- reshape2::melt(dfRoad)
dflong <- rbind(dflong, reshape2::melt(dfRail))
Note that now only one call to geom_density is needed and that the legend labels are automatic.
ggplot(dflong, aes(x = value, group = variable, fill = variable, alpha = 0.5)) +
geom_density() +
xlim(-6, 6) +
ggtitle("") +
xlab("Value") +
ylab("Density") +
scale_fill_manual(values = c(mycolRA, mycolRO)) +
theme_classic() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text = element_text(size=15),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.2,0.8), legend.direction = "vertical") +
labs(fill = "Group") +
guides(alpha = FALSE)

Specific Barplot in R ggplot

Hello I will try for a last time,
I am doing my best to draw a barplot like the following Figure:
However it seems impossible with R.
Any idea?
Thanks in advance,
Peter
Attached the code I used.
groupe2<-rep(c(rep("P",4),rep("I",4)),2)
groupe<-rep(c("PPP","PPI","PIP","PII","IPP","IPI","IIP","III"),2)
OR_A<-c(1.00,0.86,0.88,0.90,0.77,0.68,0.77,0.70)
ICinf_A<-c(NA,0.70,0.72,0.76,0.60,0.50,0.61,0.61)
ICsup_A<-c(NA,1.06,1.07,1.06,1.00,0.92,0.96,0.81)
OR_B<-c(1.00,0.97,1.01,0.81,0.73,0.69,0.61,0.58)
ICinf_B<-c(NA,0.78,0.77,0.62,0.61,0.57,0.50,0.52)
ICsup_B<-c(NA,1.20,1.28,1.05,0.81,0.82,0.71,0.65)
OR_C<-c(1.00,1.03,0.86,0.65,0.68,0.58,0.47,0.37)
ICinf_C<-c(NA,0.84,0.67,0.50,0.59,0.49,0.40,0.33)
ICsup_C<-c(NA,1.27,1.10,0.86,0.78,0.69,0.56,0.41)
Cohort<-c(rep(" PC",8), rep("RIC",8))#, rep("RIC",8))
OR<-c(OR_A,OR_B)#,OR_C)
ICinf<-c(ICinf_A,ICinf_B)#,ICinf_C)
ICsup<-c(ICsup_A,ICsup_B)#,ICsup_C)
rm(dataOR)
dataOR<-data.frame(OR,groupe,Cohort,groupe2,ICinf,ICsup)
names(dataOR)
dataOR[, "groupe"] <- factor(dataOR[, "groupe"] ,
levels = c("PPP","PPI","PIP","PII","IPP","IPI","IIP","III"))
##########
library(ggdag)
ggplot(dataOR, aes(fill=outcome, y=OR, x=groupe)) +
geom_bar(position="dodge", stat="identity", color = "gray95", size = 0.25) +
# scale_fill_brewer(palette="Blues")+
scale_fill_manual(values = RColorBrewer::brewer.pal(5, "Blues")[3:5]) +
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.4, position=position_dodge(.9))+
geom_hline(yintercept=1) +
geom_point(position = position_dodge(0.9), size = 0.5, show.legend = F) +
scale_y_continuous(expand = expand_scale(mult = c(0, 0.05))) +
facet_wrap(~groupe, nrow = 1, scales = "free_x") +
labs(fill = NULL) +
theme(legend.position = "top",
legend.key.height = unit(0.2, "cm"),
legend.background = element_rect(color = "black", size = 0.4),
axis.line = element_line(color = "black"),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_blank(),
axis.title = element_text(face = "bold"))

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