all I have used this script to get this bubble plot
library(reader)
data <- read.csv
r <- c(1,194,26302,81,69, 12,11617,55,10)
dfdata = data.frame(data,r)
dfdata
sizeRange <- c(5,20)
library(ggplot2)
ggplot(data, aes(x=0,ï..Phylum)) +
geom_point(aes(size = r,alpha = 7,colour = "red", stroke=2)) +
scale_size(range = sizeRange) +
theme_minimal()+
theme(legend.position = "none")
I am trying to reduce the axis distance for this plot. How that can be done. And also, these bubble size is not coming properly according to the "r" values.
Please help.
Thank you
Related
I used the following code to plot a packing circle graph and I want to add the numbers (values) for each bubble in addition to the text. How do I do that?
Another question is whether someone knows how to deal with a large number of categories (about 200) which makes some of the plot unreadable. Is there another visualization that might be more useful in this case?
Thanks in advance!
library(packcircles)
library(ggplot2)
library(viridis)
library(ggiraph)
packing <- circleProgressiveLayout(data$Number, sizetype='area')
data <- cbind(data, packing)
dat.gg <- circleLayoutVertices(packing, npoints=50)
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, fill=as.factor(id), colour = "black", alpha = 0.6)) +
geom_text(data = data, aes(x, y, size=Number, label = Journal)) +
scale_size_continuous(range = c(2,4)) +
theme_void() +
theme(legend.position="none")+
coord_equal()```
I am having a difficult understanding why this code works and doesn't work. I want a plot by group + specify the number of columns of my legend. Basically, the only way I can get this to work is to specify both a fill and a colour variable in the aesthetic. It seems like fill allows me to change the columns and colour changes colors of the lines, but this feels a bit kludgy. Does anyone have a good understanding of the logic here, or a better way to accomplish this goal? I’ll accept a base plot answer!
My example code is below:
# ~ Library ~ #
require(ggplot2)
# Generate example data
x=1:10
y=10:1
data = data.frame(x_data=rep(1:10,2),
y_data=c(x,y),
group=c(rep('A',length(x)),rep('B',length(y))))
# ~ Plot data ~ #
# Only this works
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group,colour=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
# This doesn't work
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
plot
# Neither does this
plot = ggplot(data,aes(x=x_data,y=y_data,colour=group)) + geom_line()
plot = plot + guides(fill = guide_legend(ncol = 2))
plot
As suggested by h-1, this works
#This works
plot = ggplot(data,aes(x=x_data,y=y_data,fill=group)) + geom_line()
plot = plot + guides(colour = guide_legend(ncol = 2))
plot
I've created a heatmap using ggplot
library(plyr)
library(scales)
guide_ind <- ddply(guide_tag[company == FALSE], .(tag), transform, rescale = rescale(count))
(p <- ggplotly(ggplot(guide_ind, aes(tag, username)) +
geom_tile(aes(fill = rescale),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
theme(axis.text.x = element_text(angle= 90, hjust=1), legend.position= "bottom") )
)
I have about 700 rows of user name, and I would like to make sure that all the usernames are visible in document so that when I produce this in markdown, it will show the names individually instead of overlapping like the picture below.
I've tried using the fig.height, and gplot heatmap, but neither has worked.
Does anyone have suggestions to how to make all data points visible on the yaxis?
I quite like the look and feel of ggplot2 and use them often to display raster data (e.g facetting over timesteps for time-varying precipitation fields is very useful).
However, I'm still wondering whether it is easily possible to bin the continuous raster values into discrete bins and assign to each bin a single colour, that is shown in the legend (as many GIS systems do).
I tried with the guide = "legend", and breaks arguments of the scale_fill_gradient option. However these affect just the legend on the side of the graph, but the plotted values are still continuous.
library(ggplot2)
data <- data.frame(x=rep(seq(1:10),times = 10), y=rep(seq(1:10),each = 10), value = runif(100,-10,10))
ggplot(data = data, aes(x=x,y=y)) +
geom_raster(aes(fill = value)) +
coord_equal() +
scale_fill_gradient2(low = "darkred", mid = "white", high = "midnightblue",
guide = "legend", breaks = c(-8,-4,0,4,8))
My question is mainly how to discretize the data that is plotted in ggplot, so that the reader of the graph can make quantitative conclusions on the values represented by the colors.
Secondly, how can I still use a diverging color palette (similar to scale_fill_gradient2), that is centered around zero or another specific value?
You should use the raster package to work with raster data. This
package provides several function to work with categorical
rasters. For example, with reclassify you can convert a continuous
file into a discrete raster. The next example is adapted from
this question:
library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
r <- reclassify(r, c(0, 500, 1,
500, 2000, 2))
On the other hand, if you want to use the ggplot2 functions, the
rasterVis package provides a simple wrapper around ggplot that
works with RasterLayer objects:
library(rasterVis)
gplot(r) +
geom_raster(aes(fill = factor(value))) +
coord_equal()
to define your own colors you can add then:
scale_fill_manual(values=c('red','green')))
The best is indeed to modify the underlying data set by manually discretizing it. Below answer is based on the answer by joran.
library(ggplot2)
set.seed(1)
data <- data.frame(x = rep(seq(1:10),times = 10),
y = rep(seq(1:10),each = 10),
value = runif(100,-10,10))
# Define category breaks
breaks <- c(-Inf,-3:3,Inf)
data$valueDiscr <- cut(data$value,
breaks = breaks,
right = FALSE)
# Define colors using the function also used by "scale_fill_gradient2"
discr_colors_fct <-
scales::div_gradient_pal(low = "darkred",
mid = "white",
high = "midnightblue")
discr_colors <- discr_colors_fct(seq(0, 1, length.out = length(breaks)))
discr_colors
# [1] "#8B0000" "#B1503B" "#D18978" "#EBC3B9" "#FFFFFF" "#C8C0DB" "#9184B7" "#5B4C93" "#191970"
ggplot(data = data, aes(x=x,y=y)) +
geom_raster(aes(fill = valueDiscr)) +
coord_equal() +
scale_fill_manual(values = discr_colors) +
guides(fill = guide_legend(reverse=T))
Update 2021-05-31:
Based on the comment by #slhck one can indeed discretize the data in the aesthetic mapping as follows:
library(ggplot2)
set.seed(1)
data <- data.frame(x = rep(seq(1:10),times = 10),
y = rep(seq(1:10),each = 10),
value = runif(100,-10,10))
# Define category breaks
breaks <- c(-Inf,-3:3,Inf)
discr_colors <- scales::div_gradient_pal(low = "darkred", mid = "white", high = "midnightblue")(seq(0, 1, length.out = length(breaks)))
# [1] "#8B0000" "#B1503B" "#D18978" "#EBC3B9" "#FFFFFF" "#C8C0DB" "#9184B7" "#5B4C93" "#191970"
ggplot(data = data, aes(x=x,y=y)) +
geom_raster(aes(fill = cut(value, breaks, right=FALSE))) +
coord_equal() +
scale_fill_manual(values = discr_colors) +
guides(fill = guide_legend(reverse=T))
I have some data here
I read the data into a data frame and then plot this data with this following code,
# Reading data from a .csv file into a data frame
df <- read.table("newcsv_file.csv",header=T,sep="\t" )
# Now melting the data frame prior to plotting
df_mlt <- melt(df, id=names(df)[1], measure=names(df)[c(2, 6, 11,16,21,26,31,36,41,46,51,106,111,116,121,126,131,136,141,146,151)], variable = "cols")
# plotting the data
plt_fit <- ggplot(df_mlt, aes(x=x,y= value, color=cols)) +
geom_point(size=2) +
geom_smooth(method = "lm", se = FALSE) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides = "rl") +
theme_bw() +
theme(legend.text=element_text(size=12), legend.title=element_text(size=12))+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14,face="bold")) +
labs(x = "x", y = "y") +
scale_color_discrete(name = "values", labels = c("0","-0.1","-0.2","-0.3","-0.4","-0.5","-0.6","-0.7","-0.8","-0.9","-1","+0.1","+0.2","+0.3","+0.4","+0.5","+0.6","+0.7","+0.8","+0.9","+1")) +
guides(colour = guide_legend(override.aes = list(size=3),nrow=2,title.position = 'top',title.hjust=0.5,legend.direction = "horizontal")) +
theme(legend.position = 'bottom', legend.margin=unit(1,"cm"),legend.background = element_rect(fill ='gray94')) +
theme(plot.margin=unit(c(0,2,0,0),"mm"))
The resulting plot looks like this, the problem here is that the right most edge of the legend is cropped.
I use +theme(legend.margin=unit(1,"cm")) but this does not seem sufficient. Could someone please let me know what I can change to display the full legend properly in the plot.
Thanks.
The code is fine. The problem is the size of your plot window. Try making it wider and you'll see the whole legend.
Also,
ggsave("plot_fit.pdf",plot_fit)
will create a pdf where the full legend is displayed.
After changing the width and height of the plot using the following code,
ggsave(file="new_png_file.png",width=22,height=21,units=c("cm"), dpi=600)
Yields a plot such as this,