Can you use text as X axis labels on a plot? I've searched and cannot see any examples. Am I trying to do something that is not possible in R? Even when I try to plot one variable. Countries is text/character - but I do not know how to set it as such
plot(Finally$Countries,Finally$RobberyPerCent, pch = 16, col = 2)
I get the error
Error in plot.window(...) : need finite 'xlim' values
In addition: There were 24 warnings (use warnings() to see them)
Thank you, my goal is to combine two variables and see if there is a basic pattern. I've been able to figure out simple linear regression (no correlation), but I'm failing at basic plotting
#Subset for Percentages
Q5DataFinal <- subset(Q5Data, select = c(RobberyPerCent, UnlawfulPerCent))
View(Q5DataFinal)
library(data.table)
Nearlythere <- setDT(Q5DataFinal, keep.rownames = TRUE)[] # turn rownames into column data
names(Nearlythere)[names(Nearlythere) == 'rn'] <- 'Countries' #renaming rn to countries
Nearlythere$Countries[] <- lapply(Nearlythere$Countries, as.character) #Changing Countries to Character
Finally <- Nearlythere
summary(Finally) #Countries saved as characters
# Attempt to create two Y axis Graph with Countries as X ticks
par(mar = c(5, 4, 4, 4) + 0.3) # Additional space for second y-axis
plot(Finally$Countries,Finally$RobberyPerCent, pch = 16, col = 2) # Create first plot
par(new = TRUE) # Add new plot
plot(Finally$Countries, Finally$UnlawfulPerCent, pch = 17, col = 3, # Create second plot without axes
axes = FALSE, xlab = "", ylab = "")
axis(side = 4, at = pretty(range(Finally$UnlawfulPerCent))) # Add second axis
mtext("UnlawfulPerCent", side = 4, line = 3) # Add second axis label
Dput is
structure(list(Countries = list("Albania", "Austria", "Bulgaria",
"Croatia", "Cyprus", "Czechia", "Finland", "Germany (until 1990 former territory of the FRG)",
"Greece", "Ireland", "Italy", "Kosovo (under United Nations Security Council Resolution 1244/99)",
"Latvia", "Lithuania", "Luxembourg", "Malta", "Montenegro",
"Romania", "Serbia", "Slovenia", "Spain", "Switzerland"),
RobberyPerCent = c(5, 6, 18, 7, 5, 23, 5, 9, 24, 9, 40, 12,
17, 18, 10, 52, 24, 33, 10, 17, 80, 2), UnlawfulPerCent = c(95,
94, 82, 93, 95, 77, 95, 91, 76, 91, 60, 88, 83, 82, 90, 48,
76, 67, 90, 83, 20, 98)), row.names = c(NA, -22L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x0000020282d01ef0>)
Do you want something like this?
par(mar = c(5, 5, 4, 2))
x <- seq(0, 5, length.out = 500)
plot(x, sin(x^2), xaxt = "n", xlab = expression("Here is X"), ylab = expression(sin(x^2)),
main = expression("My coolest plot" - sin(x^2)))
axis(1, at=0:5, labels=c("Albania", "Kosovo", "Kongo", "Germany", "Bulgaria", "Spain"))
An addition
#your dataset
countries <- list("Albania", "Austria", "Bulgaria",
"Croatia", "Cyprus", "Czechia", "Finland", "Germany (until 1990 former territory of the FRG)",
"Greece", "Ireland", "Italy", "Kosovo (under United Nations Security Council Resolution 1244/99)",
"Latvia", "Lithuania", "Luxembourg", "Malta", "Montenegro",
"Romania", "Serbia", "Slovenia", "Spain", "Switzerland")
#modify to
axis(1, at=0:21, labels=countries, cex.axis=0.5) #select cex.axis for better displaying
Related
On this page, I found an interesting plot:
Is it possible to do something similar or exactly? (Combination between treemap and ggraph library).
You can get a similar appearance with the voronoiTreemap package:
library(voronoiTreemap)
vor <- data.frame(h1 = 'World',
h2 = c('Europe', 'Europe', "Europe",
'America', 'America', 'America', 'America',
'Asia', 'Asia', 'Asia', 'Asia', 'Asia', 'Asia',
'Africa', 'Africa', 'Africa'),
h3 = c("UK", "France", "Germany",
"USA", "Mexico", "Canada", "Brazil",
"China", "India", "S Korea", "Japan", "Thailand",
"Malaysia", "Egypt", "South Africa", "Nigeria"),
color = rep(c("pink", "steelblue", "#96f8A0", "yellow"),
times = c(3, 4, 6, 3)),
weight = c(12, 10, 15, 40, 5, 7, 9, 45, 30, 20, 20, 6, 9,
8, 10, 5),
codes = c("UK", "France", "Germany",
"USA", "Mexico", "Canada", "Brazil",
"China", "India", "S Korea", "Japan", "Thailand",
"Malaysia", "Egypt", "South Africa", "Nigeria"))
vt <- vt_input_from_df(vor)
vt_d3(vt_export_json(vt))
I have the dataframe below:
ct<-structure(list(name = c("Afghanistan India", "Afghanistan India",
"Albania Kosovo", "Albania Kosovo", "Bangkok Agreement", "Bangkok Agreement",
"Bangkok Agreement", "Bangkok Agreement", "Bangkok Agreement",
"Belarus Russia (Union State)", "Belarus Russia (Union State)",
"Albania Macedonia", "Albania Macedonia", "Belarus Serbia", "Belarus Serbia",
"Belarus Ukraine", "Belarus Ukraine", "Belize Guatemala", "Belize Guatemala",
"Bhutan India"), Country = c("Afghanistan", "India", "Albania",
"Kosovo", "Bangladesh", "India", "Laos", "South Korea", "Sri Lanka",
"Belarus", "Russia", "Albania", "North Macedonia", "Belarus",
"Serbia", "Belarus", "Ukraine", "Belize", "Guatemala", "Bhutan"
), Scope = c(3, 3, 23, 23, 23, 23, 23, 23, 23, 26, 26, 6, 6,
6, 6, 6, 6, 1, 1, 5), year2 = c(2000, 2000, 2000, 2000, 1975,
1975, 1975, 1975, 1975, 1995, 1995, 2000, 2000, 2005, 2005, 1990,
1990, 2005, 2005, 2005), pta_count = c(2, 3, 8, 1, 1, 1, 1, 1,
1, 2, 2, 8, 8, 1, 4, 2, 7, 2, 3, 1)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
and I create this plot in which I modified the text displayed for the black dots.I want also to dislay the variable name inside the hover text of dots but it is not included in variables which I use for x and y axis.
# for instance
i <- 2
p<-ct %>% filter(Country==unique(ct$Country)[i]) %>%
ggplot(aes(year2,Scope))+geom_jitter()+
geom_col(aes(y=pta_count/(max(dt2$pta_count)/max(dt2$scope_ntis_ciu))),
fill="darkolivegreen",alpha=0.3,width=3)+
xlim(c(1950,2020))+
scale_y_continuous(
limits=c(0,33),
# Features of the first axis
name = "NTI Scope\n(scope measures the sum of all NTIs mentioned in a PTA,\ndot indicated one PTA)",
# Add a second axis and specify its features
sec.axis = sec_axis( ~ . * max(dt2$pta_count)/max(dt2$scope_ntis_ciu), name="PTA Count\n(green columns indicate number of PTAs\n signed in given 5-year intervall)")
)+
labs(x='',title=unique(ct$Country)[i],
subtitle = paste0('signed ',sum(ct[ct$Country=="India",]$pta_count),' PTAs in total and\nhas an average ',mean(ct[ct$Country==unique(ct$Country)[i],]$Scope),' NTI-scope index across all years'))
# create plotly object to modify
p2 <- plotly_build(p)
# now modify the text calls for each trace that this applies to
# modfiy existing tooltips
# this is the first trace (the bar chart or first geom in ggplot object)
p2$x$data[[1]]$text <- str_replace_all(p2$x$data[[1]]$text,
"year2", "Year ") %>%
str_replace_all(., fixed("Scope"),
"Count of issues ")
p2
When you made the minor changes to the hover text, I had shown a method to replace what was there. However, for this–the addition of a new variable, it's easiest to go back to the ggplot object.
BTW I had to change scope_ntis_ciu to Scope in a few places in this code.
Return to the layers that you want to change:
In the geom_jitter layer, add a call for text in aes with what you want to appear in your hover text.
Did you get an error–'can't find name'? If you do, you added the text outside of aes.
geom_jitter(aes(text = paste0("Name: ", name, "\nYear: ", year2,
"\nCount of issues: ", Scope))) +
Then your column layer:
geom_col(aes(y=pta_count/(max(pta_count)/max(Scope)),
text = paste0("Year: ", year2, "\nCount of issues: ",
pta_count/(max(pta_count)/max(Scope)))),
fill="darkolivegreen",alpha=0.3,width=3)+
When you execute this code for the ggplot object, you will be warned that ggplot is ignoring your text–that's okay, because it still keeps the information and sends it right along to plotly, where it will be used.
Now when you call the ggplotly object, you can add the tooltip.
ggplotly(p, tooltip = "text")
I have 2 dataframes and I would like to highlight the common rows
library(openxlsx)
df = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Thailand", "Thailand", "Thailand"),
Count = c(17, 15, 60, 23, 25, 60, 50, 18, 31))
df2 = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Japan", "Thailand", "Thailand"),
Count = c(17, 100, 101, 102, 103, 60, 104, 18, 31))
wb = createWorkbook()
addWorksheet(wb, "Master")
writeDataTable(wb, "Master", df2, tableStyle = "TableStyleLight9")
yellow_style = createStyle(fgFill = "#FFFF00")
x = which(abs(df2$Count) == df$Count)
y = 1:which(colnames(df2) == "Count")
addStyle(wb, sheet = "Master", style = yellow_style, rows = x+1, col = y, gridExpand = TRUE)
saveWorkbook(wb, "Master.xlsx", overwrite = TRUE)
Right now this set of codes work but it can only verify "Count" instead of the entire row.
If I want to find out the common "Count", it will work perfectly. But let's say I want to verify that the entire row is the same, how do I do it?
Here is a base R approach. Use paste to create a composite of your columns, and identify which rows have same composite in other data frame.
x = which(do.call(paste, df2) %in% do.call(paste, df))
y = 1:ncol(df2)
Assuming the dataframes have the same dimensions (same number of rows and columns) you can simply compare them via df == df2 and then check row-wise whether the result has only the value TRUE in each row:
library(tidyverse)
df = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Thailand", "Thailand", "Thailand"),
Count = c(17, 15, 60, 23, 25, 60, 50, 18, 31))
df2 = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Japan", "Thailand", "Thailand"),
Count = c(17, 100, 101, 102, 103, 60, 104, 18, 31))
eq_df <- as_tibble(df == df2)
eq_df <- eq_df %>% rowwise() %>% mutate(rows_equal = (Year & Country & Count))
The resulting eq_df indicates in last column whether the respective row in df and df2 are the same.
How can I make a graph like that? Thank you.
If I have two dataframes like this here, and their topics are the same.
You could do something like this:
library(ggplot2)
df <- data.frame(Frequency = c(300, 0, 600, 900, 0, 1000, 700,
0, 300, 400, 0, 1400, 1600, 6500,
0, -250, -500, -400, -600, -1300,
0, -1150, -100, 0, -200, 0, -2500,
-2500),
Stream = rep(c("Ne", "Pri"), each = 14),
country = c("Argentina", "Brazil", "Canada",
"France", "Germany", "India",
"Indonesia", "Italy", "Mexico",
"Phillipines", "Spain", "Thailand",
"United Kingdom", "United States"))
ggplot(df, aes(Frequency, country, fill = Stream)) +
geom_col(width = 0.6) +
labs(y = "") +
scale_x_continuous(breaks = c(-2500, 0, 2500, 3750, 5000, 6250),
labels =c(250, 0, 2500, 3750, 5000, 6250),
limits = c(-2600, 7000)) +
theme_bw() +
theme(panel.border = element_blank())
Edit
If I have two data frames like this:
df1 <- data.frame(topic = c("design", "game", "hardware", "price"),
n = c(80, 1695, 29, 53))
df1
#> topic n
#> 1 design 80
#> 2 game 1695
#> 3 hardware 29
#> 4 price 53
df2 <- data.frame(topic = c("design", "game", "hardware", "price"),
n = c(400, 1235, 290, 107))
df2
#> topic n
#> 1 design 400
#> 2 game 1235
#> 3 hardware 290
#> 4 price 107
Then I can simply rbind them together, negating the n column on df2 first and adding a column to show which data frame each value came from:
df3 <- rbind(df1, within(df2, n <- -n))
df3$origin <- rep(c("df1", "df2"), each = nrow(df1))
And when I plot, I add abs as a labeller in `scale_x_continuous to remove the negative symbols on the left half of the plot.
ggplot(df3, aes(n, topic, fill = origin)) +
geom_col() +
scale_x_continuous(labels = abs)
I am looking for some help with the given sample data of countries on one column and count on another column. I am trying a build a geo maps using ggplot showing the count and name of the country in the respective places of the map when I hover above the country. Below is the sample data given. I tried with the ggmap with the lat and long position to identify the country but not able to show the count and name of the country on hovering.
structure(list(Countries = c("USA", "India", "Europe", "LATAM",
"Singapore", "Phillipines", "Australia", "EMEA", "Malaysia",
"Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921,
707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA,
13L), class = "data.frame")
I tried the below code.
countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))
ggplotly(q)
You can change any attribute in the result from ggplotly. In this case you can set the text attribute of the 2nd trace (where you markers are defined).
plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries,
Countryprofile$count,
sep='<br />')
plotly_map
library(plotly)
library(ggmap)
Countryprofile <- structure(list(Countries = c("USA", "India", "Europe", "LATAM",
"Singapore", "Phillipines", "Australia", "EMEA", "Malaysia",
"Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921,
707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA,
13L), class = "data.frame")
countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))
plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries, Countryprofile$count, sep='<br />')
plotly_map