title: How to plot weighted means by group?
body: <p>I am looking for a straightforward way to plot weighted means by group on a line chart (each line representing a country, example provided below). </p>
  
  <p>My data has a hierarchical structure where individuals nested in countries and countries nested in years, but not all countries have surveys in each year. Here is my attempt to create a minimal data frame similar to my data:</p>
  
  <pre><code>dat &lt;- data.frame(country=rep(LETTERS, each=3)[1:20], replicate(3, sample(11, 10)), year=sample(c(1990:2000), 100, TRUE),wght=sample(c(1:3), 100, TRUE))
  </code></pre>
  
  <p>I managed to calculate weighted.means by country-year with the following code and I saved it as a matrix:</p>
  
  <pre><code>mat1&lt;-by(dat,list(dat$country,dat$year), function(x)with(x,weighted.mean(x$X1,x$wght)))[]
  </code></pre>
  
  <p>But this is how far I could get. Can somebody please help me with plotting the weighted averages by country-year from this matrix or offer an  alternative way to plot weighted averages by groups? </p>
  
  <p>To be perfectly clear, this is how I need my graph to look like:</p>
  
  <p><a href="https://i.stack.imgur.com/cxxM1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cxxM1.png" alt="Weighted average by country"></a></p>