I'm plotting different types of graphs where my legend has the colorbar of the values in my graph in log like in the image below:
The code that gives something like this is:
2Dmap <- ggplot(x, aes(L, W, fill = R)) +
geom_raster() + coord_fixed() +
scale_fill_viridis(name="R", trans = "log", direction = -1, labels=trans_format('log10',math_format(10^.x)), breaks=cycle.breaks,
limits=c(10^(floor(log10(min(x$R[x$R > 0] )))), 10^(ceiling(log10(max(x$R)))) ) )+
theme_bw() + coord_fixed(ratio=12)
2Dwmap <- 2Dwmap + theme(legend.position="bottom") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.key.width=unit(2.2, "cm")) +
theme(legend.text=element_text(size=15)) +
theme(legend.position="bottom",
title = element_text(size = 20),
axis.title.x=element_text(size = 20),
axis.title.y=element_text(size = 20),
axis.text.x = element_text(size = 18),
axis.text.y = element_text(size = 18),
legend.title = element_text(size = 18),
legend.text = element_text(size = 16))
So, I'm trying to look for a good way to avoid my limiting breaks to be cut off.
Any1 any ideas ?
Sorry I cannot share data to make a reproducible example :)
Related
I am trying to plot my ggplot with a vertical line that goes from 0 on the x-axis and straight up. I have tried to use geom_line, but I don't seem to get it to work. I would like something similar to the figure. This is my code so far.
ggplot(dat_plot, aes(x = values, y = ind, fill = "lightgrey", width = 0.5)) +
stat_summary(fun.data = quantiles_95, geom = "boxplot") +
ggtitle("Wild boar") +
ylab("") +
xlab("Effect") +
scale_y_discrete(labels = faktorer_vs) +
theme_classic() +
theme(legend.position = "none", axis.text = element_text(size = 10),
axis.title = element_text(size = 13), axis.line = element_line(colour = "black"),
plot.title = element_text(size = 18))
I'm trying to create plots where the x-axis ALSO appears above the plot as well as the y-axis to the right of the plot. Both should contain the same ticks as the normal axis, but NOT the axis text. This should result in a "box" with helpful tick-marks around the plot. I would also like to have smaller ticks in-between my major ticks (that are labelled) that do not have a label. Here is a figure I made:
I also drew in pink what I would like to achieve in R:
]3
My code for this plot:
p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40), legend.title = element_text(size=45))
p <- p + theme(text = element_text(family = "Helvetica")) + scale_x_continuous(limits=c(-0.5, 25), breaks = c(0, 2, 4, 6, 8, 24)) + theme(legend.background = element_rect(color = "black", linetype = "solid")) + scale_colour_manual(values = cbpallette)
p <- p + theme(legend.key.size = unit(2.5, "cm")) + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))
p
*** Edit ***
Here is updated code, using Stefans advise. The figure looks like so, the ticks are there, the axis are missing:
p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1, angle=45), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40))
p <- p + theme(text = element_text(family = "Helvetica")) + scale_colour_manual(values = cbpallette)
p <- p + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))
p
You could duplicate the axes using argument sec.axis = dup_axis() for both scales like so:
library(ggplot2)
ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
I'm trying to make a shot chart in which the color gradient represents the average of success in each bin.
The next script gives the count of each bin, How can I change it to represent average of success in each bin instead the count? I attach the script output chart.
#rm(list=ls())
data3<-read.csv("data10.csv",header=T)
require(jpeg)
require(grid)
court<-rasterGrob(readJPEG("nba_court.jpg"),
width=unit(1,"npc"), height=unit(1,"npc"))
require(hexbin)
require(ggplot2)
ggplot(data3, aes(x=loc_x, y=loc_y)) +
# annotation_custom(court, -247, 253, -50, 418) +
stat_binhex(bins = 18, colour = "gray", alpha = 0.8) +
scale_fill_gradientn(colours = c("cyan","yellow","red")) +
guides(alpha = FALSE, size = FALSE) +
xlim(250, -250) +
ylim(-52, 418) +
geom_rug(alpha = 0.5) +
coord_fixed() +
ggtitle("Kobe Bryant shots") +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.title = element_blank(),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold"))
DATASET SAMPLE:
data3 <- data.frame(matrix(data=c(-98,-75,-119,83,10,-103,-191,69,196,-21,-106,-127,-180,50,125,200,34,45,99,120,108,184,102,206,113,-3,93,94,164,101,82,146,108,24,56,77,67,200,250,-45,1,0,0,0,1,1,0,0,0,0,1,1,0,1,0,1,1,0,0,1),
nrow=20,ncol=3))
colnames(data3)<-c("loc_x","loc_y","shot_made_flag")
You should use stat_summary_hex and set fun=mean in order to calculate the effectiveness inside each bin:
# Create random data
set.seed(1)
data3 = data.frame(loc_x = runif(1000,-250,250),
loc_y = rnorm(1000,230,50),
shot_made_flag = rbinom(1000,1,.5))
require(hexbin)
require(ggplot2)
# The first two lines have changed (z = shot_made_flag and using fun = mean)
ggplot(data3, aes(x=loc_x, y=loc_y, z = shot_made_flag)) +
stat_summary_hex(fun = mean, bins = 18, colour = "gray", alpha = 0.8) +
scale_fill_gradientn(colours = c("cyan","yellow","red")) +
guides(alpha = FALSE, size = FALSE) +
xlim(250, -250) +
ylim(-52, 418) +
geom_rug(alpha = 0.5) +
coord_fixed() +
ggtitle("Kobe Bryant shots") +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.title = element_blank(),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold"))
Result:
Edited the full answer due to new data and to reflect the desired output (mean inside each hex cell)
I am trying to customize the strip in facet_grid function.
My graph currently looks like:
I successfully changed the background of strip to purple, however, I could not change the border color to black, even though I set the color to 'black' in the function.
Also, I want the space between text and the rectangle to be larger so that it will looks nicer. How should I achieve this?
My codes looks like:
plot.density <- ggplot(df_densityWindow, aes(x = idx, y = density, color = factor(location))) +
geom_bar(stat = 'identity', fill = 'white') +
facet_grid(marker ~ case, scales = 'free') +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF")) +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
background_grid(major = 'y', minor = "none") + # add thin horizontal lines
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(axis.title = element_text(size = 28)) +
theme(axis.text = element_text(size = 26)) +
theme(legend.text = element_text(size = 16)) +
theme(legend.title = element_text(size = 18)) +
panel_border() # and a border around each panel
plot(plot.density)
If necessary, the data could be downloaded here:
data
Thank you!
Your colour specification for strips works without error for me. The spacing between the strip text and box can be increased by setting the margin argument at the strip.text theme:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
facet_grid(Species ~ rev(Species)) +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)))
Possible debug strategy:
Reproduce code above
1A. If that doesn't work, check if your ggplot versions is ~3.2-ish
1B. If that does work, proceed to 2.
Try to remove and place back lines of plotting to see where the error occurs
I'm particularly unfamiliar with panel_border() and background_grid(), so you could try those first.
EDIT: Plot based on data and code provided
ggplot(data, aes(x = idx, y = density, colour = factor(location))) +
geom_col(fill = "white") +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
facet_grid(marker ~ case, scales = "free") +
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(strip.background = element_rect(colour = "red", fill = "#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)),
axis.title = element_text(size = 28),
axis.text = element_text(size = 26),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
# Improvised based on missing functions
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(colour = "grey90", fill = NA))
I have created a bar chart which shows the sales of products in a particular category. This is the bar chart. As you can see it is not very clear so I am trying to set limits for the Y axis.
I create the bar chart with the following line:
bakerySales <- ggplot(sales_bakery, aes(ProductName, ProductSales))+
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend =
FALSE)
I then go on to apply a theme to the bar chart using:
bakerySales <- bakerySales +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(colour = "black", size = 14, angle = 60,
hjust = 1),
axis.text.y = element_text(colour = "black", size = 14),
panel.background = element_rect(fill = "white"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.line = element_line(colour = "black", size = 1),
legend.position = "none",
plot.title = element_text(lineheight = 8, face = "bold"))
I have tried to set the limits for the y axis using:
bakerySales <- bakerySales + ylim(5000,10000)
When I do this I lose the content of the bar chart, It looks like this.
Can someone please tell me where I am going wrong.
Thanks
If you want to zoom in on specifix ylimits, you could use the coord_cartesian function. I do not have the bakerysales dataset, this is an example using mtcars data:
ggplot(mtcars, aes(x = gear, y = qsec)) +
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(200, 300))
Maybe you want
+ coord_cartesian(ylim = c(5000,10000))
df <- data.frame(x = c("a","b"), y = c(1000, 2000))
ggplot(df, aes(x=x,y=y)) +
geom_bar(stat="identity") +
coord_cartesian(ylim = c(500,3000))