How to not include arrows in ggbiplot output? - r

I have the following exemplary data:
88 0 -3.944 669.8 6.33 637.55 setosa
60 0 -3.477 651.81 6.19 618.55 setosa
4.4 0.001 -2.944 570.7 6.28 544.49 setosa
5000 0.003 -2.585 420.52 5.27 404.39 setosa
116 0.004 -2.365 761.97 6.18 714.59 setosa
300 0.008 -2.079 731.9 5.59 690.57 setosa
70 0.011 -1.942 761.97 6.36 714.59 setosa
121 0.014 -1.852 775.95 5.71 730.59 versicolor
55 0.02 -1.699 681.88 5.64 638.54 versicolor
92.1 0.028 -1.549 653.87 6.25 610.53 versicolor
75 0.041 -1.384 653.83 5.39 614.52 versicolor
20 0.065 -1.187 711.95 6.8 662.56 versicolor
10000 0.075 -1.125 394.48 4.95 380.37 virginica
130 0.085 -1.073 779 7.23 732.63 virginica
400 0.097 -1.012 662.79 5.61 628.52 virginica
99 0.111 -0.954 864.54 9.88 814.14 virginica
400 0.135 -0.87 869.49 7.06 816.06 virginica
I have the following code:
iris=read.table('file', header=FALSE)
log.ir <- (iris[, 1:6])
ir.species <- iris[, 7]
ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE)
library(devtools)
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.species, ellipse = TRUE, circle = FALSE, varname.size=0)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')
print(g)
My question is "How can I modify it so the arrows are not drawn?"

So I had to install ggbiplot via devtools and manually update package::digest before I could get your example code to reproduce, but var.axes will do the trick:
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.species,
ellipse = TRUE, circle = FALSE, varname.size=0, var.axes = F)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')

Related

ggpredict: Add significance letters to a plot using output from lsmeans

I'd like to add the significance letters to a plot using ggeffects. In my case:
# Packages
library(ggeffects)
library(dplyr)
library(glmmTMB)
library(multcomp)
library(lsmeans)
# My data set
ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/temp_ger_ds.csv")
str(ds)
#'data.frame': 140 obs. of 4 variables:
# $ temp : chr "constante" "constante" "constante" "constante" ...
# $ generation : chr "G0" "G0" "G0" "G0" ...
# $ development: int 22 24 22 27 27 24 25 26 27 18 ...
First fit the ziGamma model:
mTCFd <- glmmTMB(development ~ temp * generation, data = ds,
family = ziGamma(link = "log"))
Pairwise Comparison Post Hoc Tests:
lsm.TCFd.temp <- lsmeans(mTCFd, c("temp","generation"))
cld(lsm.TCFd.temp, Letters=letters)
temp generation lsmean SE df lower.CL upper.CL .group
constante G3 3.13 0.0180 129 3.09 3.16 a
constante G2 3.14 0.0180 129 3.11 3.18 ab
constante G0 3.19 0.0191 129 3.15 3.23 abc
constante G1 3.22 0.0180 129 3.18 3.25 bc
constante G4 3.23 0.0185 129 3.19 3.27 cd
flutuante G1 3.32 0.0352 129 3.25 3.39 cde
flutuante G3 3.34 0.0262 129 3.28 3.39 e
flutuante G0 3.36 0.0191 129 3.32 3.39 e
flutuante G4 3.36 0.0393 129 3.28 3.44 def
flutuante G2 3.47 0.0218 129 3.43 3.52 f
Now, display these letters to the plot:
ggpredict(mTCFd, terms = c("temp","generation")) %>% plot(add.data = TRUE)
But if I try:
lt<-cld(lsm.TCFd.temp, Letters=letters)
ggpredict(mTCFd, terms = c("temp","generation")) %>% plot(add.data = TRUE) %>% geom_text(aes(label = lt[,8]), vjust = -0.5)
Error in geom_text(., aes(label = lt[, 8]), vjust = -0.5) :
could not find function "geom_text"
Doesn't work! Please any help with it?
geom_text is a ggplot function. You may need to set up data for ggplot. Instead of plotting ggpredict directly, use ggpredict to get a data.frame.
I generated a new variable x_1 as x axis. You can have your own ways to get this. I just show the rough idea.
ds <- ds %>% mutate(x_1= 1+(readr::parse_number(generation)-2)*0.05 + as.integer(temp =="flutuante"),
group = generation)
df_gg <- ggpredict(mTCFd, terms = c("temp","generation")) %>%
mutate(x_1= 1+(readr::parse_number(as.character(group))-2)*0.05 + as.integer(x =="flutuante"))
df_gg %>% ggplot(aes(x = x_1, y = predicted, color = group)) +
geom_jitter(aes(y = development), data = ds, alpha = 0.25)+
geom_point()+
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.02)+
geom_text(aes(x = x_1, label = lt[, 8]), vjust = -0.5, show.legend = FALSE)+
scale_x_continuous(breaks = c(1, 2), labels = c("constante", "flutuante"))

Problems creating points on a map ggplot2/r

I have a data set of UK earthquakes that I want to plot by location on a map. (Hopefully I then want to change the size to be representative of the magnitude). I have made a map of the uk using ggmap, but I am struggling to then add the points to a map.
I however keep getting 2 errors, and cannot plot my points on the map. The errors are either
- Error: Aesthetics must be either length 1 or the same as the data (990): x, y
or
- Error in FUN(X[[i]], ...) : object 'group' not found
depending on how I try to plot the points.
this is what I have so far:
table <- data.frame(long2, lat2, mag1)
table
long2 lat2 mag1
1 -2.62 52.84 1.9
2 1.94 57.03 4.2
3 -0.24 51.16 0.6
4 -2.34 53.34 0.8
5 -3.16 55.73 2.0
6 -0.24 51.16 1.0
7 -4.11 53.03 1.5
8 -0.24 51.16 0.2
9 -0.24 51.16 1.1
10 -5.70 57.08 1.6
11 -2.40 53.00 1.4
12 -1.19 53.35 1.2
13 -1.02 53.84 1.7
14 -4.24 52.62 0.8
15 -3.23 54.24 0.3
16 -2.06 52.62 1.0
17 1.63 54.96 1.7
18 -5.24 56.05 0.7
19 -5.86 55.84 1.3
20 -3.22 54.23 0.3
21 -0.24 51.16 -1.4
22 -0.24 51.16 -0.7
23 -4.01 55.92 0.3
24 -5.18 50.08 2.3
25 -1.95 54.44 1.0
library(ggplot2)
library(maps)
w <- map_data("world", region = "uk")
uk <- ggplot(data = w, aes(x = long, y = lat, group=group)) + geom_polygon(fill = "seagreen2", colour="white") + coord_map()
uk + geom_point(data=table, aes(x=long2, y=lat2, colour="red", size=2), position="jitter", alpha=I(0.5))
Is it the way I have built my map, or how I am plotting my points? And how do I fix it?
I've made three changes to your code, and one or more of them solved the problems you were having. I'm not sure exactly which—feel free to experiment!
I named your data pdat (point data) instead of table. table is the name of a built-in R function, and it's best to avoid using it as a variable name.
I have placed both data= expressions inside the geom function that needs that data (instead of placing the data= and aes() inside the initial ggplot() call.) When I use two or more data.frames in a single plot, I do this defensively and find that it avoids many problems.
I have moved colour="red" and size=2 outside of the aes() function. aes() is used to create an association between a column in your data.frame and a visual attribute of the plot. Anything that's not a name of a column doesn't belong inside aes().
# Load data.
pdat <- read.table(header=TRUE,
text="long2 lat2 mag1
1 -2.62 52.84 1.9
2 1.94 57.03 4.2
3 -0.24 51.16 0.6
4 -2.34 53.34 0.8
5 -3.16 55.73 2.0
6 -0.24 51.16 1.0
7 -4.11 53.03 1.5
8 -0.24 51.16 0.2
9 -0.24 51.16 1.1
10 -5.70 57.08 1.6
11 -2.40 53.00 1.4
12 -1.19 53.35 1.2
13 -1.02 53.84 1.7
14 -4.24 52.62 0.8
15 -3.23 54.24 0.3
16 -2.06 52.62 1.0
17 1.63 54.96 1.7
18 -5.24 56.05 0.7
19 -5.86 55.84 1.3
20 -3.22 54.23 0.3
21 -0.24 51.16 -1.4
22 -0.24 51.16 -0.7
23 -4.01 55.92 0.3
24 -5.18 50.08 2.3
25 -1.95 54.44 1.0")
library(ggplot2)
library(maps)
w <- map_data("world", region = "uk")
uk <- ggplot() +
geom_polygon(data = w,
aes(x = long, y = lat, group = group),
fill = "seagreen2", colour = "white") +
coord_map() +
geom_point(data = pdat,
aes(x = long2, y = lat2),
colour = "red", size = 2,
position = "jitter", alpha = 0.5)
ggsave("map.png", plot=uk, height=4, width=6, dpi=150)

plotting with specific values for heatmap in pheatmap

I have a data frame like this:
gene s1 s2 s3
1 -3.83 -8.17 -8.59
2 0.33 -4.51 -7.27
3 0.15 -5.26 -6.2
4 -0.08 -6.13 -5.95
5 -1.15 -4.82 -5.75
6 -0.99 -4.11 -4.85
7 0.42 -4.18 -4.54
8 -0.32 -3.43 -4.4
9 -0.72 -3.37 -4.39
I want to make a heatmap using pheatmap where if anything is below -4 it should be green and anything over +4 should be red and everything in between should red/green shades. I also don't want to scale my data and no clustering. I have this code so far in R:
d <- read.table("test.txt", header = TRUE, sep = "\t", row.names = 1, quote = "")
pheatmap(as.matrix(d), # matrix
scale = "none", # z score scaling applied to rows
cluster_cols=FALSE, # do not cluster columns
cluster_rows = FALSE,
treeheight_row=0, # do not show row dendrogram
show_rownames=FALSE, # do not show row names i.e gene names
main = "test.txt",
color = colorRampPalette(c("#0016DB","#FFFFFF","#FFFF00"))(50),
)
How can I plot this with the color scheme I mentioned above.
Thanks
d <-read.table(text="gene s1 s2 s3
1 -3.83 -8.17 -8.59
2 0.33 -4.51 -7.27
3 0.15 -5.26 -6.20
4 -0.08 -6.13 -5.95
5 -1.15 -4.82 -5.75
6 -0.99 -4.11 -4.85
7 0.42 -4.18 -4.54
8 -0.32 -3.43 -4.40
9 -0.72 -3.37 -4.39", header=T)
library(pheatmap)
my_colors <- c(min(d),seq(-4,4,by=0.01),max(d))
my_palette <- c("green",colorRampPalette(colors = c("green", "red"))
(n = length(my_colors)-2), "red")
pheatmap(as.matrix(d),
scale = "none",
cluster_cols=FALSE,
cluster_rows = FALSE,
treeheight_row=0,
show_rownames=FALSE,
main = "test.txt",
color = my_palette,
breaks = my_colors)
Created on 2019-05-29 by the reprex package (v0.3.0)

Heatmap using ggplot2 in r

My Data looks like this:
data1 <- read.table(text = "District Block IE FE AOE CLE CS
A A1 4.87 17.54 13.85 9.01 45.27
B B1 8.19 20.83 14.59 7.04 50.65
C C1 8.71 19.16 16.54 8.24 52.65
D D1 2.43 11.77 11.51 6.96 32.67
E E1 6.85 13.54 14.54 5.7 40.63
F F1 7.02 19.96 13.96 3.82 44.76
G G1 2.55 11.64 8.74 5.06 27.99
H H1 9.81 20.2 12.62 5.95 48.58
I I1 6.56 15.49 12.32 8.08 42.45
J J1 9.47 22.86 25 22.73 80.06
K K1 10.2 20.18 20.14 20.06 70.58
L L1 9.52 14.86 16.95 18.23 59.56", header = TRUE)
I have created data matrix from the dataframe.My initial code looks like this
row.names(data1)<-data1$Column1
data1<-select(data1,-c(1))
data1<-data.matrix()
data1_heatmap<-heatmap(data1,Rowv = NA,Colv = NA,col=heat.colors(256),scale = "none",margins = c(12,3))
Whenever I am using the above code,it plots for the whole sheet.
I have 2 issues:
I need to show the cell values which are present in the data matrix.
Also i need to specify a color range in each column from IE to CS.For example,in IE column less the 4.87 is red,6.56 to 6.85 is orange and green for greater than 8.17.So basically user defined range for each column.
Try this with ggplot2 (starting with the original data1):
library(ggplot2)
library(reshape2)
row.names(data1)<-data1$Column1
data1<-select(data1,-c(1))
data1 <- melt(data1, id='Block')
data1$value <- cut(data1$value,breaks = c(-Inf,4.87, 6.56, 6.58, 8.17, 14, 19, 21, Inf),right = FALSE) # bin IE variable
ggplot(data = data1, aes(x = Block, y = variable)) +
geom_tile(aes(fill = value), colour = "white") +
scale_fill_brewer(palette = "PRGn")

Alpha not applied for all points in ggplot scatterplot

I have been trying to produce a scatter plot with two levels of alpha applied to dots that are above or below a score threshold. To do so, I am storing the alpha value for each point in a vector, item_alpha, within the data frame and supplying this vector as the argument for alpha in my call to geom_point:
library( ggplot2 );
library( scales );
one.data <- read.table("test.data", header = TRUE)
p1 <- ggplot( data = one.data )
p1 <- p1 + geom_point( aes( plot_X, plot_Y, colour = log10_p_value, size = plot_size, alpha = item_alpha ) )
p1 <- p1 + scale_colour_gradientn( colours = c("red", "yellow", "green", "blue"), limits = c( min(one.data$log10_p_value), max(one.data$log10_p_value)));
p1 <- p1 + geom_point( aes(plot_X, plot_Y, size = plot_size), shape = 21, fill = "transparent", colour = I (alpha ("black", 0.6) ));
p1 <- p1 + scale_size( range=c(5, 30)) + theme_bw();
one.x_range = max(one.data$plot_X) - min(one.data$plot_X);
one.y_range = max(one.data$plot_Y) - min(one.data$plot_Y);
p1 <- p1 + xlim(min(one.data$plot_X) one.x_range/10,max(one.data$plot_X)+one.x_range/10);
p1 <- p1 + ylim(min(one.data$plot_Y)one.y_range/10,max(one.data$plot_Y)+one.y_range/10);
p1
However, it seems alpha is only being set properly for the eight points with the smaller value, while the remaining points remain opaque. I've consulted the ggplot documentation, played with the examples and tried some other variations which have mostly produced various errors and I'm really hoping someone will have some insight on this! Thanks in advance!
Contents of test.data:
"plot_X" "plot_Y" "plot_size" "log10_p_value" "item_alpha"
5.326 3.194 4.411 -27.3093 0.6
-2.148 7.469 3.434 -12.3487 0.6
-6.14 -2.796 3.062 -22.8069 0.6
3.648 6.091 3.597 -15.5032 0.6
0.356 -6.925 3.95 -10.4754 0.6
5.532 -0.135 3.246 -19.2883 0.6
3.794 -2.279 3.557 -16.4438 0.6
-3.784 1.42 2.914 -17.9687 0.6
-7.645 -1.571 3.163 -12.4498 0.6
-1.526 -4.756 3.509 -10.8972 0.6
-6.461 2.293 2.962 -13.4306 0.6
-5.806 0.983 4.38 -24.5422 0.6
-3.592 0.769 2.971 -17.8119 0.6
0.127 3.572 3.603 -11.4277 0.6
-0.566 0.706 3.77 -13.0952 0.3
2.25 -2.604 0.845 -11.7949 0.3
-7.845 -0.927 3.21 -12.6408 0.3
1.084 -6.691 3.654 -10.7319 0.3
-3.546 6.46 2.994 -11.6777 0.3
-5.478 -0.645 4.256 -17.7344 0.3
-6.251 -0.418 4.273 -19.29 0.3
-3.855 5.969 3.236 -10.9057 0.3
0.345 0.971 3.383 -11.5973 0.6
0.989 0.345 2.959 -10.8252 0.6
You're using a distinctly base plotting approach with ggplot2, which is obviously not the right way to go. Here are two options:
dat <- read.table(text = "plot_X plot_Y plot_size log10_p_value item_alpha
5.326 3.194 4.411 -27.3093 0.6
-2.148 7.469 3.434 -12.3487 0.6
-6.14 -2.796 3.062 -22.8069 0.6
3.648 6.091 3.597 -15.5032 0.6
0.356 -6.925 3.95 -10.4754 0.6
5.532 -0.135 3.246 -19.2883 0.6
3.794 -2.279 3.557 -16.4438 0.6
-3.784 1.42 2.914 -17.9687 0.6
-7.645 -1.571 3.163 -12.4498 0.6
-1.526 -4.756 3.509 -10.8972 0.6
-6.461 2.293 2.962 -13.4306 0.6
-5.806 0.983 4.38 -24.5422 0.6
-3.592 0.769 2.971 -17.8119 0.6
0.127 3.572 3.603 -11.4277 0.6
-0.566 0.706 3.77 -13.0952 0.3
2.25 -2.604 0.845 -11.7949 0.3
-7.845 -0.927 3.21 -12.6408 0.3
1.084 -6.691 3.654 -10.7319 0.3
-3.546 6.46 2.994 -11.6777 0.3
-5.478 -0.645 4.256 -17.7344 0.3
-6.251 -0.418 4.273 -19.29 0.3
-3.855 5.969 3.236 -10.9057 0.3
0.345 0.971 3.383 -11.5973 0.6
0.989 0.345 2.959 -10.8252 0.6",header = TRUE)
dat$alpha_grp <- ifelse(dat$item_alpha == 0.6,'High','Low')
#If you want a legend; although you can suppress the legend
# here if you want.
ggplot(data = dat,aes(x = plot_X,y = plot_Y)) +
geom_point(aes(alpha = alpha_grp)) +
scale_alpha_manual(values = c(0.3,0.6))
#If you don't care about a legend
ggplot() +
geom_point(data = dat[dat$alpha_grp == 'High',],
aes(x = plot_X,y = plot_Y),alpha = 0.6) +
geom_point(data = dat[dat$alpha_grp == 'Low',],
aes(x = plot_X,y = plot_Y),alpha = 0.3)

Resources