I'm trying to create a dotplot where countries are listed on my Y axis from A-Z top to bottom. The medal count will be the X axis for each of the four plots, one each for gold, silver, bronze, and total. Of course, ggplot prefers to plot countries from Z-A and despite reading all about the problem, I haven't resolved the issue. I appreciate any straightforward help on both the coding and comprehension fronts.
mdat <- melt(raw, value.name = "Count", variable.name = "Place", id.var = "Country")
mdat[, "Place"] <- factor(mdat[, "Place"], levels=c("Gold", "Silver", "Bronze", "Total"))
##I know my problem is likely on or around the above line ##
plot1 <- ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
geom_point() +
facet_grid(.~Place) + theme_bw()+
scale_colour_manual(values=c("#FFCC33", "#999999", "#CC6600", "#000000"))
print(plot1)
Algeria Gold 4
Argentina Gold 5
Armenia Gold 1
Algeria Silver 2
Argentina Silver 5
Armenia Silver 2
Algeria Bronze 4
Argentina Bronze 2
Armenia Bronze 0
You have to sort the levels of Country before you plot. Also, there is no Total level the data you provided. The following appraoch should give you the desired result:
Reading the data (including a Total level for the Place variable):
mdat <- read.table(text="Country Place Count
Algeria Gold 4
Argentina Gold 5
Armenia Gold 1
Algeria Silver 2
Argentina Silver 5
Armenia Silver 2
Algeria Bronze 4
Argentina Bronze 2
Armenia Bronze 0
Algeria Total 10
Argentina Total 12
Armenia Total 3", header=TRUE)
Sorting the levels of the Country variable:
mdat$Country <- factor(mdat$Country,levels=sort(unique(mdat$Country),decreasing=TRUE))
Getting your Place variable in the correct order:
levels(mdat$Place) <- c("Bronze"=3,"Gold"=1,"Silver"=2,"Total"=4)
mdat$Place <- as.numeric(mdat$Place)
mdat$Place <- as.factor(mdat$Place)
levels(mdat$Place) <- c("Gold","Silver","Bronze","Total")
Creating the plot:
ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
geom_point(size=4) +
facet_grid(.~Place) + theme_bw()+
scale_colour_manual(values=c("#FFCC33","#999999","#CC6600","#000000"))
which gives the following plot:
As you melted your data already, I suspect that there is no Total variable in the raw dataframe. You can calculte that with:
raw$Total <- rowSums(..specify the Gold, Silver & Bronze columns here..)
Related
This question already has answers here:
ggplot2: sorting a plot
(5 answers)
How to force specific order of the variables on the X axis?
(1 answer)
Closed last month.
Good morning,
I'm trying to use ggplot with a data frame but I faced an issue. My ggplot doesn't take consideration about the function arrange on my data frame.
Here is my code :
data()
pop <- population[population$year == 1995, ]
pop <- pop[1:10, ]
pop %>%
ggplot(aes(x = country, y = population)) +
geom_point()
pop <- pop %>%
arrange(population)
pop %>%
ggplot(aes(x = country, y = population)) +
geom_point()
I would like that my graph would be arranged according to the population, so at the first place, the country with the lowest population, at the second place, the country with the second lowest population and so on. But ggplot doesn't match my graph as expected.
I have this data frame :
country year population
<chr> <int> <int>
1 Anguilla 1995 9807
2 American Samoa 1995 52874
3 Andorra 1995 63854
4 Antigua and Barbuda 1995 68349
5 Armenia 1995 3223173
6 Albania 1995 3357858
7 Angola 1995 12104952
8 Afghanistan 1995 17586073
9 Algeria 1995 29315463
10 Argentina 1995 34833168
But my graph is ordered by alphabetical order :
Do you have any idea to make it by population number?
I have this data:
country name value
<chr> <chr> <dbl>
1 Germany Jd 7.1
2 Germany Jc 8.4
3 Germany Ne 1.3
4 France Jd 8.3
5 France Jc 12
6 France Ne 3.7
and I would like to plot it in two groups of bars (with three columns each). Ordered the same as it is in the dataframe: First Germany, second France and the order of the columns Jd, Jc, Ne.
I did:
p <- ggplot(data, aes(x = country, y = value)) +
geom_bar(aes(fill = name), width=0.7, position = position_dodge(width=0.7), stat='identity')
but I get the plot in a different order: first France, then Germany and the order of the columns Jc, Jd, Ne. (seems to be ordered alphabetically).
How can I order the bars in the way I want?
Probably one of the simplest ways to take control on sorting is to convert as.factor() your ordering columns and define the levels, you'll override any other default ordering:
library(ggplot2)
data$country <- factor( data$country, levels = c("Germany", "France"))
data$name <- factor( data$name, levels = c("Jd", "Jc", "Ne"))
ggplot(data, aes(x = country, y = value,fill = name)) +
# moved the aes() all together, nothing related to the question
geom_bar(width=0.7, position position_dodge(width=0.7), stat='identity')
With data:
data <- read.table(text = "
country name value
Germany Jd 7.1
Germany Jc 8.4
Germany Ne 1.3
France Jd 8.3
France Jc 12
France Ne 3.7",header = T)
I'm trying to plot this dataset with ggplot2, putting the name of each country in each line geom_line() and with the x axis (Year) and the y axis (with the relevant data from each country).
The DataSet to Edit
This is what I have so far. I wanted to include the name of the country in each line. The problem is that each country has its data in a separate column.
If you want to use ggplot you should bring your data into a "longer" format. Using package tidyr:
df %<>%
pivot_longer(cols=matches("[^Year]"),
names_to="Country",
values_to="Value")
gives you
# A tibble: 108 x 3
Year Country Value
<dbl> <chr> <dbl>
1 1995 Argentina 4122262
2 1995 Bolivia 3409890
3 1995 Brazil 36276255
4 1995 Chile 2222563
5 1995 Colombia 10279222
6 1995 Costa_Rica 1611055
7 1997 Argentina 4100563
8 1997 Bolivia 3391943
9 1997 Brazil 35718095
10 1997 Chile 2208382
Based on this it is easy to plot a line for each country using ggplot2:
ggplot(df, aes(x=Year, y=Value, color=Country)) +
geom_line()
You kind of answered your question. You require the package reshape to bring all countries into a single column.
Year<-c(1991,1992,1993,1994,1995,1996)
Argentina<-c(235,531,3251,3153,13851,16513)
Mexico<-c(16503,16035,3516,3155,30351,16513)
Japan<-c(1651,868416,68165,35135,03,136816)
df<-data.frame(Year,Argentina,Mexico,Japan)
library(reshape2)
df2<- melt(data = df, id.vars = "Year", Cont.Val=c("Argentina","Mexico","Japan"))
library(ggplot2)
ggplot(df2, aes(x=Year, y=value, group=variable, color=variable))+
geom_line()
I have a tibble where the column names of original df were given by values in variable col that i melted into long format using id.vars=Country to get this using melt. This is to plot the different values of AGR_LogLabProd, MIN_LogLabProd, MAN_LogLabProd by year on the same x-axis .
CHN4
Country Year variable value
---------------------------
1 CHN 1958 AGR_LogLabProd 14.81782
2 CHN 1959 AGR_LogLabProd 14.61870
3 CHN 1960 AGR_LogLabProd 14.41969
4 CHN 1961 AGR_LogLabProd 14.28257
5 CHN 1958 MIN_LogLabProd 13.67850
6 CHN 1959 MIN_LogLabProd 14.24685
7 CHN 1960 MIN_LogLabProd 14.57734
8 CHN 1961 MIN_LogLabProd 14.59046
9 CHN 1958 MAN_LogLabProd 13.29359
10 CHN 1959 MAN_LogLabProd 13.86194
11 CHN 1960 MAN_LogLabProd 14.19243
12 CHN 1961 MAN_LogLabProd 14.20556
I use ggplot(CHN4, aes(x=Year, y=value))+geom_line()but its giving me a strange plot (given in the attached image) , not seperate lines for each variable in the variable column as expected . Any clue to whats going wrong?
This is a pretty common problem. You need to include a grouping variable. If you want to use color for every different level, you would use
library(ggplot2)
ggplot(CHN4, aes(x=Year, y=value, color = variable)) +
geom_line()
but if you don't care for colors, you can do
library(ggplot2)
ggplot(CHN4, aes(x=Year, y=value, group = variable)) +
geom_line()
df
Date Team Country Score
1/1/2012 TeamA Germany 10
1/2/2012 TeamA Germany 25
1/3/2012 TeamA Germany 50
1/1/2012 TeamB France 10
1/2/2012 TeamB France 70
1/3/2012 TeamB France 50
I would like to create facet wrap graph with ggplot to have title and also put country on the left side of each plot as shown on the image. Is this doable with ggplot?
ggplot(df,aes(Date, Score, group=Team, colour=Team))+
geom_point(size=0.5) +
geom_smooth(method="lm", se=T, size=1) +
facet_wrap(~Team, scale="free")
I define
dat.text
Team Country x y
3 TeamA Germany 3 30
4 TeamB France 3 15
Using geom_text
p + geom_text(aes(x, y, label=Country, group=NULL),data=dat.text,
angle =90,family = "mono",size=12)
+ theme(legend.position = "none")
The idea: I use a new data frame with three pieces of information and suuply it to geom_text.
ggplot do the rest of job.
Coordinates (x,y)
The faceted variable levels : Team
The labels to be supplied : Country
To put the text in the right of plot , I choose
dat.text$x <- tail(originalData$Date,1)