If you run the code below you will a line graph. How can I change the color of the point at x = 2 to RED and increase it's size?
In this case the on the graph the point at (.6) where x = 2 would be highlighted red and made bigger.
Here is my code:
library("ggplot2")
data<-data.frame(time= c(1,2,3), value = c(.4,.6,.7))
ggplot(data, aes( x = time, y=value) ) + geom_line() + geom_point(shape = 7,size = 1)
Thank you!
If your dataset is small you could do this:
> library("ggplot2")
> data<-data.frame(time= c(1,2,3), value = c(.4,.6,.7),point_size=c(1,10,1),cols=c('black','red','black'))
> ggplot(data, aes( x = time, y=value) ) + geom_line() + geom_point(shape = 7,size = data$point_size, colour=data$cols)
Makes:
Also I would not advise calling your data frame data
In addition to #Harpal's solution, you can add two more columns to your data frame where pointsize and -color is specified according to particular conditions:
df <- data.frame(time= c(1,2,3), value = c(.4,.6,.7))
# specify condition and pointsize here
df$pointsize <- ifelse(df$value==0.6, 5, 1)
# specify condition and pointcolour here
df$pointcol <- ifelse(df$value==0.6, "red", "black")
ggplot(df, aes(x=time, y=value)) + geom_line() + geom_point(shape=7, size=df$pointsize, colour=df$pointcol)
You may change ifelse(df$value==0.6, 5, 1) to meet any criteria you like, or you use a more complex approach to specifiy more conditions to be met:
df <- data.frame(time= c(1,2,3), value = c(.4,.6,.7))
df$pointsize[which(df$value<0.6)] <- 1
df$pointsize[which(df$value>0.6)] <- 8
df$pointsize[which(df$value==0.6)] <- 5
df$pointcol[which(df$value<0.6)] <- "black"
df$pointcol[which(df$value>0.6)] <- "green"
df$pointcol[which(df$value==0.6)] <- "red"
ggplot(df, aes(x=time, y=value)) + geom_line() + geom_point(shape=7, size=df$pointsize, colour=df$pointcol)
Related
I have used a simple CSV table and made a plot with the desired colors and dots, but I cannot find the solution to connect the dots with a line.
#----Import data----#
DS <- read_csv("https://raw.githubusercontent.com/Iqbalpr/Tugas-Kuliah--UIN/main/Data%20Skripsi%20Gender%20%2B%20Negara%20(CSV).csv")
View(DS)
ncol(DS)
nrow(DS)
#----Check and convert column type----#
str(DS) # Check Column
DS$ID <- as.factor(DS$ID )
DS$Gender <- as.factor(DS$Gender)
DS$Tahun <- as.integer(DS$Tahun)
DS$Inflasi <- as.numeric(DS$Inflasi)
DS$Pengangguran <- as.numeric(DS$Pengangguran)
DS$`GDP growth rate` <- as.numeric(DS$`GDP growth rate`)
DS$`GDP per Capita` <- as.numeric(DS$`GDP per Capita`)
str(DS) # Check Column Again
#----Plot----#
p <- ggplot(DS) + aes(x = Tahun, y = AHH, group = Negara, color = Negara) + geom_point()
p
enter image description here
Now I want the dots connected with the same color as the dots and I use this code:
p <- ggplot(DS) + aes(x = Tahun, y = AHH, group = Negara, color = Negara) + geom_point() + geom_line()
p
but I get a very strange line like this :
enter image description here
What am I doing wrong?
This happens because you have two values per country because of your Gender column which will result in the graph you have. An option is to use facet_wrap to plot it for each Gender like this:
library(ggplot2)
p <- ggplot(DS) +
aes(x = Tahun, y = AHH, group = Negara, color = Negara) +
geom_point() +
geom_line() +
facet_wrap(~Gender)
p
Output:
I am creating tanglegrams with the following code:
library(ggtree)
library(ape)
tree1 <- read.tree(text='(((A:4.2,B:4.2):3.1,C:7.3):6.3,D:13.6);')
tree2 <- read.tree(text='(((B:4.2,A:4.2):3.1,C:7.3):6.3,D:13.6);')
p1 <- ggtree(tree1)
p2 <- ggtree(tree2)
d1 <- p1$data
d2 <- p2$data
d2$x <- max(d2$x) - d2$x + max(d1$x) + 1
pp <- p1 + geom_tree(data=d2)
dd <- bind_rows(d1, d2) %>%
filter(!is.na(label))
final_plot <- pp + geom_line(aes(x, y, group=label), data=dd, color='grey')
What I want to do is to color the lines based on the position of the nodes. In other words, if the line is straight, meaning that they have the same position in both trees, the color should be x, while if they have changed, it should be y.
Something like this:
It would also be nice to get a legend for this to explain the colors.
You can construct a column in dd that checks if the line will be horizontal. Here I grouped by label and checked whether the number of unique id's is 1. Then you use that column to the color argument in the aes of the line.
dd <- dd %>% group_by(label) %>% mutate(is.horiz = n_distinct(node) == 1)
pp +
geom_line(aes(x, y, group=label, color = is.horiz), data=dd) +
scale_color_manual(values = c('TRUE' = "lightblue", 'FALSE' = "purple")) +
theme(legend.position = c(.9,.9)) +
labs(color = 'Horizontal Nodes')
You can play around with the colors of the lines and the names of everything.
library(tidyverse)
library(ggQC)
set.seed(5555)
Golden_Egg_df <- data.frame(month = 1:12,
egg_diameter = rnorm(n = 12, mean = 1.5, sd = 0.2))
Golden_Egg_df$egg_diameter[3] <- 2.5
Example data is generated above... to create the plot shown below. I want all points above or below the red lines highlighted, preferably with a circle around said point, in our example the third point.
I know I can accomplish this adding a geom_point() with an ifelse statement. I don't know how to strip out the upper 'red line value' and 'lower red line' values from stat_QC() to allow me to utilize my proposed method.
Here's hoping you know how and can provide the answer.
XmR_Plot <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
geom_point() + geom_line() +
stat_QC(method = "XmR")
Initial plot:
library(ggplot2)
p <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
geom_point() + geom_line() +
stat_QC(method = "XmR")
Here is one option where we use ggplot_build to extract the data from the red lines. You can read more about a ggplot_build object here: https://rud.is/books/creating-ggplot2-extensions/demystifying-ggplot2.html#the-ggplot_built-object
pb <- ggplot_build(p)
thres <- range(pb$data[[3]]$yintercept) # you need to inspect pb$data to find the right element
thres contains y-values of the red lines.
thres
#[1] 0.7319105 2.3820961
If you now want to highlight only the point above (or below) these values, add another point layer with a subset of the initial data
p + geom_point(
data = subset(Golden_Egg_df,
egg_diameter > max(thres) | egg_diameter < min(thres)),
shape = 21,
size = 4,
col = "red"
)
So I'm making a histogram of the months, but the x-axis goes from 0.5 to 12.5. Does anyone know how I can fix this to 1 - 12 (as they represent the months?
x<-c(1,2,3,4,5,6,6,7,8,9,10,11,12)
qplot(x,geom='histogram',fill=I("red"), col=I("darkred"),xlab="Maand",ylab="Hoeveelheid",bins=12)
You can pass x as.factor.
library(ggplot2)
x <- c(1,2,3,4,5,6,6,7,8,9,10,11,12)
x <- as.data.frame(x)
ggplot(x, aes(as.factor(x))) +
geom_bar(fill = "red", color = "darkred") +
xlab("Maand") +
ylab("Hoeveelheid")
You can try
library(tidyverse)
tibble(x = c(1,2,3,4,5,6,6,7,8,9,10,11,12)) %>%
ggplot(aes(x)) +
geom_histogram(binwidth = 1, color="white") +
scale_x_continuous(breaks = 1:12)
In base R you can try
hist(c(1,2,3,4,5,6,6,7,8,9,10,11,12))
I have a ggplot with a geom_text():
geom_text(y = 4, aes(label = text))
The variable text has the following format:
number1-number2
I want to know if it is possible to define a color for the number1 and another color for number2 (example: red and green color)
Thanks!
One way is if you have for example the label texts of number1 and number2 as separate columns in the data frame:
ggplot(data, aes(x,y)) + geom_text(label=data[,3], color="red", vjust=0) + geom_text(label=data[,4], color="blue", vjust=1)
You may also try annotate:
# data for plot
df <- data.frame(x = 1:5, y = 1:5)
# data for annotation
no1 <- "number1"
no2 <- "number1"
x_annot <- 4
y_annot <- 5
dodge <- 0.3
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
annotate(geom = "text", x = c(x_annot - dodge, x_annot, x_annot + dodge), y = y_annot,
label = c(no1, "-", no2),
col = c("red", "black", "green")) +
theme_classic()
I defined the labels and positions outside the annotate call, which possibly makes it easier to generate these variables more dynamically, e.g. if "number1" in fact could be calculated from the original data set, or positions be based on range of x and y.